Tool for creating isolated Python environments
The common use-cases:
- Handling different versions of same libraries per each service
- Same plus no access to site-packages
In all these cases, virtualenv can help. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).
Extension scripts for virtualenv
- Organizes all of your virtual environments in one place
- Wrappers for managing your virtual environments (create, delete, copy)
- Use a single command to switch between environments
- Tab completion for commands that take a virtual environment as argument
Tool for switching between different versions of Python interpreter
Intercepts Python commands using shim executables injected into your PATH, determines which Python version has been specified by your application, and passes your commands along to the correct Python installation
PyEnv plugin that provides features to manage virtualenvs and conda environments for Python on UNIX-like systems
Features:
- additional commands activate, deactivate, ...
- auto activation and deactivation on directory enter/leave events (using .python-version file)
PyEnv plugin which provides a pyenv virtualenvwrapper command to manage virtualenvs with virtualenvwrapper
virtualenv + virtualenvwrapper
sudo -H pip install virtualenv
~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
mkdir -p $PROJECT_HOME
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
~/.bashrc
export PATH="$HOME/.pyenv/bin:$PATH"
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV=1
eval "$(pyenv init -)"
source ~/.bashrc
git clone https://github.com/yyuu/pyenv-virtualenvwrapper.git $(pyenv root)/plugins/pyenv-virtualenvwrapper
Satisfy pyenv build requirements
sudo apt-get install -y \
make \
build-essential \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
wget \
curl \
llvm \
libncurses5-dev \
libncursesw5-dev \
xz-utils \
libffi-dev \
libssl1.0-dev
Install some basic Python version for virtualenvwrapper and install it inside
pyenv install <basic_python_version>
pyenv global <basic_python_version>
pip install virtualenvwrapper
~/.bashrc
pyenv virtualenvwrapper_lazy
pyenv update
rm -rf $(pyenv root)
sudo -H pip uninstall -y virtualenv virtualenvwrapper
Remove added ~/.bashrc records related to virtualenv/virtualenvwrapper/pyenv
-
Install latest CPython 2.7
pyenv install 2.7-dev
-
Create new project and environment (both with the same name project_name)
mkproject <project_name>
-
Set Python version
pyenv shell 2.7-dev
-
Working on a project ...
-
Exit environment
source deactivate
-
Enter environment workon <project_name>
workon <project_name>
-
Working on a project ...
- Switch to project directory
cd <project_path>
- Freeze requirements based on imports (if needed)
pipreqs .
- Set Python version required by the project
pyenv shell 2.7-dev
- Create virtual environment (if needed)
mkvirtualenv -r <requirements_file_path> <virtualenv_name>
- Bind an existing virtualenv to an existing project
or just
setvirtualenvproject <virtualenv_path> <project_path>
if environment activated and PWD is a project root directorysetvirtualenvproject
- Working on a project ...
- Exit environment
source deactivate
- Set Python version for the project
pyenv shell 2.7-dev
- Create temporary virtual environment
mktmpenv
- Working ...
- Exit environment (environment folder will be deleted on deactivation)
source deactivate