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

时间:2022-11-21 00:23:55

I would like to install a nodejs script (lessc) into a virtualenv.

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

How can I do that ?

我怎样才能做到这一点 ?

Thanks

谢谢

Natim

Natim

4 个解决方案

#1


15  

I like shorrty's answer, he recommended using nodeenv, see: is there an virtual environment for node.js?

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

I followed this guide: http://calvinx.com/2013/07/11/python-virtualenv-with-node-environment-via-nodeenv/

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

All I had to do myself was:

我所要做的就是:

. ../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

#2


14  

Here is what I used so far, but it may be optimized I think.

这是我到目前为止所使用的,但我认为它可能会被优化。

Install nodejs

安装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

Install npm (Node Package Manager)

安装npm(节点包管理器)

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

Install lesscss

安装lesscss

npm install less -g

When you activate your virtualenv you can use lessc

当您激活virtualenv时,您可以使用lessc

#3


10  

I created a bash script to automate Natim's solution.

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

Makes sure your Python virtualenv is active and just run the script. NodeJS, NPM and lessc will be downloaded and installed into your virtualenv.

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

http://pastebin.com/wKLWgatq

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://*.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"

#4


3  

I will provide my generic solution to works with Gems and NPMs inside a virtualenv Gems and Npm support to be customized via env settings : GEM_HOME and npm_config_prefix

我将提供我的通用解决方案,以便在virtualenv Gems和Npm支持中使用Gems和NPM,通过env设置进行自定义:GEM_HOME和npm_config_prefix

You can stick the snippet below in your postactivate or activate script ( it s more matter if you use virtualenvwrapper or not)

你可以在你的postactivate或激活脚本中粘贴下面的片段(如果你使用virtualenvwrapper则更重要)

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

Now , inside your virtualenv all libs installed via gem install or npm -g install will be installed in your virtualenv and binary added in your PATH if you are using virtualenvwrapper you can make the change global to all your virtualenv if you modify the postactivate living inside your $VIRTUALENVWRAPPER_HOOK_DIR

现在,在你的virtualenv中,所有通过gem install或npm -g install安装的lib将被安装在你的virtualenv和你的PATH中添加的二进制文件中如果你使用virtualenvwrapper,如果你修改postactivate生活在内部,你可以对你所有的virtualenv进行全局更改你的$ VIRTUALENVWRAPPER_HOOK_DIR

This solution don't cover installing nodejs inside the virtualenv but I think that it s better to delegate this task to the packaging system (apt,yum,brew..) and install node and npm globally

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

Edit :

编辑:

I recently created 2 plugin for virtualenvwrapper to do this automatically. There is one for gem and npm :

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

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

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

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

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

#1


15  

I like shorrty's answer, he recommended using nodeenv, see: is there an virtual environment for node.js?

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

I followed this guide: http://calvinx.com/2013/07/11/python-virtualenv-with-node-environment-via-nodeenv/

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

All I had to do myself was:

我所要做的就是:

. ../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

#2


14  

Here is what I used so far, but it may be optimized I think.

这是我到目前为止所使用的,但我认为它可能会被优化。

Install nodejs

安装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

Install npm (Node Package Manager)

安装npm(节点包管理器)

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

Install lesscss

安装lesscss

npm install less -g

When you activate your virtualenv you can use lessc

当您激活virtualenv时,您可以使用lessc

#3


10  

I created a bash script to automate Natim's solution.

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

Makes sure your Python virtualenv is active and just run the script. NodeJS, NPM and lessc will be downloaded and installed into your virtualenv.

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

http://pastebin.com/wKLWgatq

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://*.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"

#4


3  

I will provide my generic solution to works with Gems and NPMs inside a virtualenv Gems and Npm support to be customized via env settings : GEM_HOME and npm_config_prefix

我将提供我的通用解决方案,以便在virtualenv Gems和Npm支持中使用Gems和NPM,通过env设置进行自定义:GEM_HOME和npm_config_prefix

You can stick the snippet below in your postactivate or activate script ( it s more matter if you use virtualenvwrapper or not)

你可以在你的postactivate或激活脚本中粘贴下面的片段(如果你使用virtualenvwrapper则更重要)

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

Now , inside your virtualenv all libs installed via gem install or npm -g install will be installed in your virtualenv and binary added in your PATH if you are using virtualenvwrapper you can make the change global to all your virtualenv if you modify the postactivate living inside your $VIRTUALENVWRAPPER_HOOK_DIR

现在,在你的virtualenv中,所有通过gem install或npm -g install安装的lib将被安装在你的virtualenv和你的PATH中添加的二进制文件中如果你使用virtualenvwrapper,如果你修改postactivate生活在内部,你可以对你所有的virtualenv进行全局更改你的$ VIRTUALENVWRAPPER_HOOK_DIR

This solution don't cover installing nodejs inside the virtualenv but I think that it s better to delegate this task to the packaging system (apt,yum,brew..) and install node and npm globally

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

Edit :

编辑:

I recently created 2 plugin for virtualenvwrapper to do this automatically. There is one for gem and npm :

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

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

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

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

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