我想在虚拟环境中安装一个nodejs脚本(lessc)。
我该怎么做?
谢谢。
Natim
我想在虚拟环境中安装一个nodejs脚本(lessc)。
我该怎么做?
谢谢。
Natim
我喜欢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
这是我目前使用的代码,但我认为它可以进行优化。
安装Node.js
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(Node 包管理器)
/absolute/path/to/the/virtualenv/bin/activate
curl https://npmjs.org/install.sh | sh
安装 lesscss
npm install less -g
当你激活虚拟环境后,你就可以使用lessc
sudo npm install less -g
安装二进制文件到/usr/local/bin/lessc
。 - JuanPablo我创建了一个Bash脚本来自动化Natim的解决方案。
确保您的Python虚拟环境处于活动状态,然后只需运行该脚本。NodeJS、NPM和Lessc将被下载并安装到您的虚拟环境中。
#!/bin/sh
#
# This script will download NodeJS, NPM and lessc, and install them into you Python
# virtualenv.
#
# Based on a post by Natim:
# https://dev59.com/cmox5IYBdhLWcg3ww3AD
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"
GEM_HOME
和npm_config_prefix
来自定义Gems和Npm支持。您可以将以下代码片段粘贴到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
gem install
或npm -g install
安装的所有库都将安装在你的虚拟环境中,并添加到你的PATH
中。
如果你正在使用virtualenvwrapper
,你可以通过修改位于$VIRTUALENVWRAPPER_HOOK_DIR
内的postactivate
来使这个更改对所有虚拟环境生效。
lessc
: http://lesscss.org/ -Virtualenv
是用于 Python 环境的,它在环境中提供了一些可执行文件,我想在这个环境中安装lessc
。 - Natim