All notes
Virtualenv

Basic


sudo pip install virtualenv

virtualenv venv
# Packages installed in this environment will live under venv/lib/pythonX.X/site-packages/.
# venv/bin/python is the specified python to be used.

# -p, --python
virtualenv --python=/opt/python-3.3/bin/python venv
# same as
export VIRTUALENV_PYTHON=/opt/python-3.3/bin/python
virtualenv venv

virtualenv -p=python3 venv

# NOTE: venv/bin/active has no x permission by default.
# It is recommended to use "source pathTo/activate" instead of "pathTo/activate".
source venv/bin/activate

deactivate

Configuration

Except command-line options, virtualenv also looks for a standard ini config file.

Best Practices

Proper way to install pip/virtualenv

  1. Download virtualenv source tar from website.
  2. Unpack the source tarball, run to create a clean bootstrap environment to "bootstrap" others. All of your virtual environments will automatically contain pip and distribute.
  3. Using pip, install virtualenv into that bootstrap environment.

mkdir ~/venv/bootstrap

# Create the first "bootstrap" environment.
python virtualenv-$VERSION/virtualenv.py ~/venv/bootstrap

source ~/venv/bootstrap/bin/activate

pip install virtualenvwrapper
export WORKON_HOME=~/Envs
./virtualenvwrapper.sh

# Now you can use your "bootstrap" environment to create more:
mkvirtualenv py-env1
mkvirtualenv py-env2

virtualenvwrapper

virtualenvwrapper provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place. It provides tab-completion on environment names.

workon also deactivates whatever environment you are currently in, so you can quickly switch between environments.

Installation:


pip install virtualenvwrapper
pip install virtualenvwrapper-win # windows
export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh

Usage:


# This creates the venv folder inside ~/Envs.
mkvirtualenv venv
# Work on a virtual environment:
workon venv

# Creates the virtual environment, and also a project directory inside $PROJECT_HOME, which is cd-ed into when you workon myproject.
mkproject myproject
workon myproject

# Still the same.
deactivate
# To delete:
rmvirtualenv venv

Other commands:


# List all of the environments.
lsvirtualenv

# Navigate into the directory of the currently activated virtual environment, so you can browse its site-packages, for example.
cdvirtualenv

# Like the above, but directly into site-packages directory.
cdsitepackages

lssitepackages

With virtualenv-burrito, you can have a working virtualenv + virtualenvwrapper environment in a single command. Only python2 supported recently.

FAQ

Error: No module named virtualenvwrapper

StackOverflow: no module named virtualenvwrapper.

When this error comes out along with the wrong python PATH like "/usr/bin/python" (you actually want "/usr/local/bin/python3"), you can set the environment var:


export VIRTUALENVWRAPPER_PYTHON="/usr/local/bin/python3"

autoenv

When you cd into a directory containing a file named ".env", autoenv automagically executes the file.

Autoenv overrides cd. If you already do this, invoke autoenv_init within your custom cd after sourcing activate.sh.

Autoenv can be disabled via unset cd.

wcfNote: currently it doesn't support MSDOS.


git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc

Recommended .env

kormoc.com: autoenv and virtualenv.


BASE_PATH=`dirname "${BASH_SOURCE}"`
PWD=`pwd`

if [[ "${BASE_PATH}" == "${PWD}" ]]
then
    if [[ -e venv/bin/activate ]]
    then
        source venv/bin/activate
    fi
fi

FAQ

How to create venv offline

What it does when creating venv?

Go to virtualenv.py, I found that it just installs the three tools, but with a set of specific environment variables.


env = {
    "PYTHONPATH": pythonpath, # pythonpath = ''.
    "JYTHONPATH": pythonpath,  # for Jython \lt 3.x
    "PIP_FIND_LINKS": findlinks, # Comes from search_dirs
    "PIP_USE_WHEEL": "1",
    "PIP_ONLY_BINARY": ":all:",
    "PIP_PRE": "1",
    "PIP_USER": "0",
}
if not download:
    env["PIP_NO_INDEX"] = "1"

try:
    # Here cmd and SCRIPT are: "pip install setuptools pip wheel"
    # cmd = 'venv/bin/python - setuptools pip wheel' 
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)

So to go offline:


# --extra-search-dir can be set multiple times, then it produces a list
virtualenv --extra-search-dir path/to/setuptoolsPackages --no-download venv

A test. argparse is used in virtualenv for parsing arguments.


# Download pip, setuptools and wheel package to current dir.
ls
# argparse-1.4.0-py2.py3-none-any.whl  pip-8.1.1-py2.py3-none-any.whl  setuptools-20.7.0-py2.py3-none-any.whl  wheel-0.29.0-py2.py3-none-any.whl

# Offline version.
# Set extra-search-dir to current dir.
time virtualenv --extra-search-dir ${PWD} --no-download venv
# New python executable in /home/me/wcftest/venv/bin/python3.5
# Also creating executable in /home/me/wcftest/venv/bin/python
# Installing setuptools, pip, wheel...done.
# 
# real	0m3.670s
# user	0m1.350s
# sys	0m0.161s

# On-line.
time virtualenv venv2
# New python executable in /home/me/wcftest/venv2/bin/python3.5
# Also creating executable in /home/me/wcftest/venv2/bin/python
# Installing setuptools, pip, wheel...done.
# 
# real	0m8.419s
# user	0m1.624s
# sys	0m0.179s

Mathematical formulae powered by MathJax.