Skip to content
Search

Virtualenv

lastmod 2025-02-14created 2015-03-0620 edits

virtualenv is a tool to create isolated Python environments.

How to install

Max OSX

On Mac OSX, it can be installed via brew as follows.

$ brew update  
$ brew install pyenv-virtualenv

Add the following to your profile and you're done.

if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

How to use

To create a new development environment (dependency environment), enter it in the form pyenv virtualenv {PYTHON_VERSION} {VIRTUAL_ENV_NAME}.

The output looks like this

yourui-MacBook-Pro:~ your$ pyenv virtualenv 2.7.11 your-pyenv-2.7.11
Collecting virtualenv
  Downloading virtualenv-15.0.1-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 235kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.1
You are using pip version 7.1.2, however version 8.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
New python executable in /Users/your/.pyenv/versions/2.7.11/envs/your-pyenv-2.7.11/bin/python2.7
Also creating executable in /Users/your/.pyenv/versions/2.7.11/envs/your-pyenv-2.7.11/bin/python
Installing setuptools, pip, wheel...done.
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/your/.pyenv/versions/2.7.11/envs/your-pyenv-2.7.11/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /Users/your/.pyenv/versions/2.7.11/envs/your-pyenv-2.7.11/lib/python2.7/site-packages

Once everything is created, proceed as below, the same way as using pyenv.

yourui-MacBook-Pro:~ your$ pyenv versions
  system
* 2.7.11 (set by PYENV_VERSION environment variable)
  2.7.11/envs/your-pyenv-2.7.11
  your-pyenv-2.7.11

yourui-MacBook-Pro:~ your$ pyenv shell your-pyenv-2.7.11
pyenv-virtualenv: deactivate
pyenv-virtualenv: activate your-pyenv-2.7.11
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

To check the currently applied environment information, enter the following.

$ virtualenv versions

The virtual environment can be manually enabled and disabled with the following commands.

  • pyenv activate
  • pyenv deactivate

Install path

If the environment name is set to your-pyenv-2.7.11 as in the installation example above, the Python install path is as follows.

/Users/your/.pyenv/versions/your-pyenv-2.7.11/

Tip

pyenv prompt display

The currently applied Python environment information is shown on the left side of the prompt as follows.

(your-pyenv-2.7.11) yourui-MacBook-Pro:~ your$

If this bothers you, you can disable it as follows.

export PYENV_VIRTUALENV_DISABLE_PROMPT=1

activate_this.py

A file used as execution code for isolation in Python:

# -*- coding: utf-8 -*-
"""Activate virtualenv for current interpreter:
Use exec(open(this_file).read(), {'__file__': this_file}).
This can be used when you must use an existing Python interpreter, not the virtualenv bin/python.
"""
import os
import site
import sys

try:
    abs_file = os.path.abspath(__file__)
except NameError:
    raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")

bin_dir = os.path.dirname(abs_file)
base = bin_dir[: -len("__BIN_NAME__") - 1]  # strip away the bin part from the __file__, plus the path separator

# prepend bin to PATH (this file is inside the bin directory)
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
os.environ["VIRTUAL_ENV"] = base  # virtual env is right above bin directory

# add the virtual environments libraries to the host python import mechanism
prev_length = len(sys.path)
for lib in "__LIB_FOLDERS__".split(os.pathsep):
    path = os.path.realpath(os.path.join(bin_dir, lib))
    site.addsitedir(path.decode("utf-8") if "__DECODE_PATH__" else path)
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]

sys.real_prefix = sys.prefix
sys.prefix = base

__BIN_NAME__, __LIB_FOLDERS__, and __DECODE_PATH__ are placeholders that are substituted when the script above is invoked.

Troubleshooting

prompt changing will be removed from future release

pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

Simply put, this appears because the pyenv-virtualenv environment changed. Don't change it.

Local Download

Pyenv-virtualenv-b952f57.tar.gz
Latest commit b952f57
Download: 2016-05-04

See also

  • pyenv: Python version management.
  • Virtualenv: Python package dependency management.
  • autoenv: Automatic environment (Environments) setup.
  • venv: Python venv

Favorite site

References


  1. Pyenv_virtualenv_autoenv_-_Python_dev_env.pdf ↩

author Zer0 sitemap