Virtual Environments#

A short overview about python virutal environment tool. For more detailed informations see pipenv

pip#

The p ackage i nstaller for p ython (pip) has in in itself a feature to save and install python modules automatically. All installed modules of the current enviroment can be saved into a file called requirements.txt.

pip freeze > requirements.txt

if such a file exist, all modules can automatically be installed in the current python environemnt.

pip install -r requirements.txt

More information about pip can be found at: PIP

But there are some missing features.

  • Version depended modules are difficult to manage

  • The notion of a virtual environemnt is completely missing

Overview#

Because pip only manages packages many virtual environment tools existg in python. It is always recommended to use a virtual environemnt for a project to not run into compatibility issues especially if working on multiple projects in parallel and multiple developers on the same code.

Existing virtual environments systems, there are confliciting standards. It is recommended to not use multiple packages in parallel.

conda#

Conda is an open source package management system and a environment management system which comes with Anaconda or miniconda. One major difference with pip is that conda install already built packages in contrast to pip which install packages from source and build them locally (sometimes packages are installed from binary wheels when available thought).

Howerver, Conda must be preferred over pip for data sciences oriented projects because it provides commonly used data sciences libraries and tools which are built using optimized, hardward-specific libraries which speed up performance without code changes.

Furthermore, the built-in environment management system allows to share environments to other people across different platforms, which supports the reproducibility of research workflows.

pipenv#

Aims to combine Pipfile, pip and virtualenv into one command on the command-line. The virtualenv directory typically gets placed in ~/.local/share/virtualenvs/XXX, with XXX being a hash of the path of the project directory. This is different from virtualenv, where the directory is typically in the current working directory. pipenv is meant to be used when developing Python applications (as opposed to libraries). For more informations see: pipenv.

pew#

pew acts as a wrapper and you only need to remember this command once you start working with it as it can be used for all commands that we would need.

virtualenv#

A very popular tool that creates isolated Python environments for Python libraries. It works by installing files in a directory eg: env/, and then modifying the PATH environment variable to prefix it with a custom bin directory eg: env/bin/. An exact copy of the python or python3 binary is placed in this directory, but Python is programmed to look for libraries relative to its path first, in the environment directory. Once activated, you can install packages in the virtual environment using pip.

Can be installed using pip install virtualenv.

Virtualenv commands#

  • virtualenv -p {PATH_TO_PYENV_PYTHON_VERSION} {ENV_NAME}: Create a new virtual env named {ENV_NAME} with the python version specified with the path. Note that this will create a folder at the current location for the virtual env.

  • {ENV_NAME}/Scripts/activate.bat: Activate the virtual environnement.

  • deactivate: deactivate the virtual environnement.

pyenv#

Used to isolate Python versions. For example, you may want to test your code against Python 2.7, 3.6, 3.7 and 3.8, so you’ll need a way to switch between them. Once activated, it prefixes the PATH environment variable with ~/.pyenv/shims, where there are special files matching the Python commands (python, pip). These are not copies of the Python-shipped commands; they are special scripts that decide on the fly which version of Python to run based on the PYENV_VERSION environment variable, or the .python-version file, or the ~/.pyenv/version file. pyenv also makes the process of downloading and installing multiple Python versions easier, using the command pyenv install.

Allow to install different version of python. Can be installed using pip install pyenv-win. Don’t forget to add {PYENV_PATH}/bin and {PYENV_PATH}/shims to your path.

Pyenv commands#

  • pyenv versions: list installed version of python.

  • pyenv version: indicate the current version of python.

  • pyenv install --list: list all python version available for the installation.

  • pyenv install {VERSION}: install the designated python version. Note that all python versions will be installed in {PYENV_PATH}/versions/

  • pyenv local {VERSION}: Set or show the local application-specific Python version.

  • pyenv global {VERSION}: Set or show the global Python version.

pyenv_virtualenv#

A plugin for pyenv, to allow you to use pyenv and virtualenv at the same time conveniently. However, if you’re using Python 3.3 or later, pyenv-virtualenv will try to run python -m venv if it is available, instead of virtualenv. You can use virtualenv and pyenv together without pyenv-virtualenv, if you don’t want the convenience features.

virtualenvwrapper#

A set of extensions to virtualenv. It gives you commands like mkvirtualenv, lssitepackages, and especially workon for switching between different virtualenv directories. This tool is especially useful if you want multiple virtualenv directories.

pyenv-virtualenvwrapper#

A plugin for pyenv, to conveniently integrate virtualenvwrapper into pyenv.

venv#

A package shipped with Python 3, which you can run using python3 -m venv. It serves the same purpose as virtualenv, but only has a subset of its features. virtualenv continues to be more popular than venv, especially since the former supports both Python 2 and 3.

Python PIP Virtual Environment