简体   繁体   中英

How to install lessc and nodejs in a Python virtualenv?

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

How can I do that ?

Thanks

Natim

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

I followed this guide: 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

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

Install 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)

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

Install lesscss

npm install less -g

When you activate your virtualenv you can use lessc

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

Makes sure your Python virtualenv is active and just run the script. NodeJS, NPM and lessc will be downloaded and installed into your 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"

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

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

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

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

Edit :

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM