我想将一个nodejs脚本(lessc)安装到virtualenv中。
我该怎么做?
由于
Natim
答案 0 :(得分:15)
我喜欢shorrty的回答,他建议使用nodeenv,请参阅: is there an virtual environment for 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
答案 1 :(得分:14)
这是我到目前为止所使用的内容,但我认为可能会进行优化。
安装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(节点包管理器)
/absolute/path/to/the/virtualenv/bin/activate
curl https://npmjs.org/install.sh | sh
安装lesscss
npm install less -g
当您激活virtualenv时,您可以使用lessc
答案 2 :(得分:10)
我创建了一个bash脚本来自动化Natim的解决方案。
确保您的Python virtualenv处于活动状态并运行该脚本。 NodeJS,NPM和lessc将被下载并安装到您的virtualenv中。
#!/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"
答案 3 :(得分:3)
我将提供我的通用解决方案,以便在virtualenv中使用Gems和NPM
Gems和Npm支持可通过env设置进行自定义:GEM_HOME
和npm_config_prefix
您可以将下面的代码段粘贴到postactivate
或activate
脚本中(如果您使用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
安装的所有库将安装在您的PATH
中添加的virtualenv和二进制文件中
如果您使用virtualenvwrapper
,如果您修改postactivate
$VIRTUALENVWRAPPER_HOOK_DIR
,则可以对您的所有版本进行全局更改
此解决方案不包括在virtualenv中安装nodejs,但我认为最好将此任务委托给打包系统(apt,yum,brew ..)并安装node和npm global
编辑:
我最近为virtualenvwrapper创建了2个插件来自动执行此操作。有一个用于gem和npm: