关于node.js:如何在Python virtualenv中安装lessc和nodejs?

How to install lessc and nodejs in a Python virtualenv?

我想将nodejs脚本(lessc)安装到virtualenv中。

我该怎么做?

谢谢

Natim


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

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

我要做的就是:

1
2
3
4
. ../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

1
2
3
4
5
6
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(节点程序包管理器)

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

安装lesscss

1
npm install less -g

激活virtualenv时,可以使用lessc


我创建了一个bash脚本来自动化Natim的解决方案。

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

http://pastebin.com/wKLWgatq

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/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"


我将提供我的通用解决方案以在virtualenv中与Gems和NPM一起使用
ruby和Npm支持可通过环境设置进行自定义:GEM_HOMEnpm_config_prefix

您可以将以下代码段粘贴在postactivateactivate脚本中(是否使用virtualenvwrapper更为重要)

1
2
3
4
5
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 installnpm -g install安装的所有库都将安装在virtualenv中,并将二进制文件添加到PATH
如果您使用的是virtualenvwrapper,则可以通过修改$VIRTUALENVWRAPPER_HOOK_DIR

内部的postactivate来对所有virtualenv进行全局更改

此解决方案不涉及在virtualenv中安装nodejs,但我认为最好将此任务委托给打包系统(apt,yum,brew ..)并在全局安装node和npm。

编辑:

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

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

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