- multiple python installation - multiple installations of different Python versions on the same machine
- virtual environments - independent environments containing Python packages of specific versions. this allows different versions of the same package to be installed on different virtual environments
3 tools for working with these, and when you might need each one:
|
python tool |
manages multiple python versions |
manages virtual environments |
|---|---|---|
|
venv/pyvenv (DEPRECATED) |
✘ |
✔ |
|
pyenv |
✔ |
✘ |
|
pyenv-virtualenv |
✘ |
✔ |
|
TODO |
TODO | |
|
✔ |
✔ |
pyenv & pyenv-virtualenv is often used together
venv
Click here to expand...
From Python 3.3+ the
venvpackage is included. It is ideal for creating lightweight virtual environments.Up until Python 3.6 a script called
pyvenvwas also included as a wrapper aroundvenv, but this has been deprecated. It will be completely removed in Python 3.8. The exact same functionality is available when usingvenv, and any existing documentation should be updated. For anyone interested you can read the reasons behind depreciatingpyvenv.
venvis used to create a new environment via the terminal command:
$ python3 -m venv directory-name-to-createactivated with:
$ source name-given/bin/activateand deactivated with simply:
$ deactivateIf you need to remove the environment completely after deactivating it, you can run:
$ rm -r name-givenBy default, the environment it creates will be the current version of Python you are using. If you are writing documentation, and want the additional safety that the correct version of Python is being used by your reader you can specify the major and minor version number in the command, like so:
$ python3.6 -m venv example-three-sixIf the reader is using a version other than 3.6 then the command will not be successful and will indicate in its error message. However, any patch version (for example 3.6.4) will work.
When the environment is active, any packages can be installed to it via
pipas normal. By default, the newly created environment will not include any packages already installed on the machine. Aspipitself will not necessarily be installed on the machine. It is recommended to first upgradepipto the latest version, usingpip install —upgrade pip.Projects will commonly have a
requirements.txtfile specifying its dependencies. This allows the shortcut commandpip install -r requirements.txtcommand to quickly install all packages to the newly created virtual environment. They will only exist in the virtual environment. It will not be available when it is deactivated but will persist when it is reactivated.If you do not need to use additional versions of Python itself, then this is all you need to create isolated, project specific, virtual environments.
pyenv
Click here to expand...
If you wish to use multiple versions of Python on a single machine, then
pyenvis a commonly used tool to install and switch between versions. This is not to be confused with the previously mentioned depreciatedpyvenvscript. It does not come bundled with Python and must be installed separately.The
pyenvdocumentation includes a great description of how it works, so here we will look simply at how to use it.Firstly we will need to install it. If using Mac OS X, we can do this using Homebrew, else consider other installation options.
$ brew update$ brew install pyenvNext, add the following towards the bottom of your shell scripts to allow
pyenvto automatically change versions for you:
eval ”$(pyenv init -)”To do, open your in use shell script, via
$ ~/.zshrc,$ ~/.bashrcor$ ~/.bash_profileand copy and paste the above line in.Running
pyenv versionswill show which Python versions are currently installed, with a*next to the one currently in use.pyenv versionshows this directly, andpython —versioncan be used to verify this.To install an additional version, say
3.4.0, simply usepyenv install 3.4.0.
pyenvlooks in four places to decide which version of Python to use, in priority order:
- The
PYENV_VERSIONenvironment variable (if specified). You can use thepyenv shellcommand to set this environment variable in your current shell session.- The application-specific
.python-versionfile in the current directory (if present). You can modify the current directory’s.python-versionfile with thepyenv localcommand.- The first
.python-versionfile found (if any) by searching each parent directory, until reaching the root of your filesystem.- The global version file. You can modify this file using the
pyenv globalcommand. If the global version file is not present, pyenv assumes you want to use the “system” Python. (In other words, whatever version would run if pyenv weren’t in yourPATH.)When setting up a new project that is to use Python 3.6.4 then
pyenv local 3.6.4would be ran in its root directory. This would both set the version, and create a.python-versionfile, so that other contributors’ machines would pick it up.The full description of
pyenvcommands is one to bookmark.
pyenv and venv
Click here to expand...
When working with Python 3.3+ we now know both how to install and switch between different versions of Python, and how to create new virtual environments.
As an example, let’s say we were setting up a project that was to use Python 3.4.
First we could set our local version using
pyenv local 3.4.0.If we then ran
python3 -m venv example-projecta new virtual environment would be set up underexample-project, using our locally enabled Python 3.4.0.We activate using
source example-project/bin/activateand can start working.Next we could optionally document that a collaborator should use
python3.4 -m venv <name>. This means even if a collaborator was not using pyenv thepython3.4command would error if their Python version was not the same major and minor version (3 and 4), as we intended.Alternatively, we could choose to simply specify that 3.4.0 was to be used, and instruct
python3 -m venv <name>. If we believe that any version greater than 3.4 is acceptable, then we also may choose to usepython3overpython3.4, as if the collaborator was using 3.6 then they would otherwise also receive an error. This is a project specific decision.
pyenv-virtualenv
Click here to expand...
pyenvcan be used to install both Python 2 and 3 versions. However, as we have seen,venvis limited to versions of Python greater than 3.3.
pyenv-virtualenvis a tool to create virtual environments integrated withpyenv, and works for all versions of Python. It is still recommended to use the official Pythonvenvwhere possible. But if, for example, you’re creating a virtual environment based on2.7.13, then this complimentspyenv.It also works well with Anaconda and Miniconda
condaenvironments if you are already using those. A tool calledvirtualenvalso exists. It’s not covered here, but it’s linked at the end.After installing
pyenvit can next be installed using Homebrew (or alternatives) as so:
$ brew install pyenv-virtualenvNext in your
.zshrc,.bashrc, or.bash_profile(depending on which shell you use) add the following towards the bottom:
eval ”$(pyenv init -)“eval ”$(pyenv virtualenv-init -)”This allows
pyenvto activate and deactivate environments automatically when moving directories.To create a new virtual environment, use:
$ pyenv virtualenv <version> <name-to-give-it>// for example$ pyenv virtualenv 2.7.10 my-virtual-env-2.7.10
// this would create a virtual env directory in $(pyenv root)/versions/{version}/envsExisting environments can be listed with:
$ pyenv virtualenvsActivated/ deactivated with:
$ pyenv activate <name>$ pyenv deactivateAt the time of writing, when using
activatethe warningprompt changing will be removed from future releasewill be displayed. This is expected and refers only to the(env-name)being displayed in your shell, not the use of theactivatecommand itself.Installing requirements works as described in
venv. Unlike invenvarm -rcommand is not needed to remove an environment, anuninstallcommandexists.