如何在Python的virtualenv中安装lessc和nodejs?

我想安装一个nodejs脚本(lessc)到virtualenv。

我怎样才能做到这一点 ?

谢谢

Natim

我喜欢shorrty的答案,他build议使用nodeenv,请参阅: 是否有node.js的虚拟环境?

我遵循这个指南: http : //calvinx.com/2013/07/11/python-virtualenv-with-node-environment-via-nodeenv/

我所要做的只是:

 . ../bin/activate # switch to my Python virtualenv first pip install nodeenv # then install nodeenv (nodeenv==0.7.1 was installed) nodeenv --python-virtualenv # Use current python virtualenv npm install -g less # install lessc in the virtualenv 

这是我迄今为止使用的,但是我认为可能会进行优化。

安装nodejs

 wget http://nodejs.org/dist/v0.6.8/node-v0.6.8.tar.gz tar zxf node-v0.6.8.tar.gz cd node-v0.6.8/ ./configure --prefix=/absolute/path/to/the/virtualenv/ make make install 

安装npm(节点包pipe理器)

 /absolute/path/to/the/virtualenv/bin/activate curl https://npmjs.org/install.sh | sh 

安装lesscss

 npm install less -g 

当你激活你的virtualenv你可以使用lessc

我创build了一个bash脚本来自动化Natim的解决scheme。

确保你的Python virtualenv处于活动状态,并运行脚本。 NodeJS,NPM和lessc将被下载并安装到你的virtualenv。

http://pastebin.com/wKLWgatq

 #!/bin/sh # # This script will download NodeJS, NPM and lessc, and install them into you Python # virtualenv. # # Based on a post by Natim: # http://stackoverflow.com/questions/8986709/how-to-install-lessc-and-nodejs-in-a-python-virtualenv NODEJS="http://nodejs.org/dist/v0.8.3/node-v0.8.3.tar.gz" # Check dependencies for dep in gcc wget curl tar make; do which $dep > /dev/null || (echo "ERROR: $dep not found"; exit 10) done # Must be run from virtual env if [ "$VIRTUAL_ENV" = "" ]; then echo "ERROR: you must activate the virtualenv first!" exit 1 fi echo "1) Installing nodejs in current virtual env" echo cd "$VIRTUAL_ENV" # Create temp dir if [ ! -d "tmp" ]; then mkdir tmp fi cd tmp || (echo "ERROR: entering tmp directory failed"; exit 4) echo -n "- Entered temp dir: " pwd # Download fname=`basename "$NODEJS"` if [ -f "$fname" ]; then echo "- $fname already exists, not downloading" else echo "- Downloading $NODEJS" wget "$NODEJS" || (echo "ERROR: download failed"; exit 2) fi echo "- Extracting" tar -xvzf "$fname" || (echo "ERROR: tar failed"; exit 3) cd `basename "$fname" .tar.gz` || (echo "ERROR: entering source directory failed"; exit 4) echo "- Configure" ./configure --prefix="$VIRTUAL_ENV" || (echo "ERROR: configure failed"; exit 5) echo "- Make" make || (echo "ERROR: build failed"; exit 6) echo "- Install " make install || (echo "ERROR: install failed"; exit 7) echo echo "2) Installing npm" echo curl https://npmjs.org/install.sh | sh || (echo "ERROR: install failed"; exit 7) echo echo "3) Installing lessc with npm" echo npm install less -g || (echo "ERROR: lessc install failed"; exit 8) echo "Congratulations! lessc is now installed in your virtualenv" 

我将提供我的通用解决scheme,以便在virtualenv中使用Gem和npm_config_prefixnpm_config_prefix支持可以通过env设置进行定制: GEM_HOMEnpm_config_prefix

您可以将下面的代码片段粘贴到您的postactivateactivate脚本中(如果您使用virtualenvwrapper则更为重要)

 export GEM_HOME="$VIRTUAL_ENV/lib/gems" export GEM_PATH="" PATH="$GEM_HOME/bin:$PATH" export npm_config_prefix=$VIRTUAL_ENV export PATH 

现在,在你的virtualenv里面,通过gem install或者npm -g install所有库都将被安装在你的virtualenv和二进制文件中,如果你使用的是virtualenvwrapper那么你可以把你的virtualenv变成全局的,如果你修改了postactivate里面的您的$VIRTUALENVWRAPPER_HOOK_DIR

这个解决scheme不包括在virtualenv里面安装nodejs,但是我认为把这个任务委托给打包系统(apt,yum,brew ..)并且在全局安装node和npm会更好

编辑:

我最近为virtualenvwrapper创build了2个插件来自动执行此操作。 有一个gem和NPM:

http://pypi.python.org/pypi/virtualenvwrapper.npm

http://pypi.python.org/pypi/virtualenvwrapper.gem