Install pyenv-virtualenv using the Homebrew package manager for Mac OS X. To upgrade pyenv-virtualenv in the future, use upgrade instead of install . Add pyenv virtualenv-init to your shell to enable auto-activation of virtualenvs.
If you are working with your personal projects and not installing pipenv, I recommend installing pyenv-virtualenv. If you are working in a team or with more than one system, I recommend you to install pipenv which I am covering next.
Pipenv adds a number of higher level features than virtualenv, namely dependency management. Virtualenv doesn't manage dependencies, all it does is provide isolated environment to install dependencies.
Pyenv is used to manage different Python versions, whereas Pipenv is used to manage Python packages.
Pyenv and virtualenv are very different tools that work in different ways to do different things:
Pyenv is a bash extension - will not work on Windows - that intercepts your calls to python, pip, etc., to direct them to one of several of the system python tool-chains. So you always have all the libraries that you have installed in the selected python version available - as such it is good for users who have to switch between different versions of python.
VirtualEnv, is pure python so works everywhere, it makes a copy of, optionally a specific version of, python and pip local to the activate environment which may or may not include links to the current system tool-chain, if it does not you can install just a known subset of libraries into that environment. As such it is almost certainly much better for testing and deployment as you know exactly which libraries, at which versions, are used and a global change will not impact your module.
Note that from Python 3.3 onward there is a built in implementation of VirtualEnv called venv (with, on some installations a wrapper called pyvenv - this wrapper is deprecated in Python 3.6), which should probably be used in preference. To avoid possible issues with the wrapper it is often a good idea to use it directly by using /path/to/python3 -m venv desired/env/path
or you can use the excellent py
python selector on windows with py -3 -m venv desired/env/path
. It will create the directory specified with desired/env/path
configure and populate it appropriately. In general it is very much like using VirtualEnv.
There are a number of tools that it is worth mentioning, and considering, as they can help with the use of one or more of the above:
Short version:
virtualenv
allows you to create local (per-directory), independent python installations by cloning from existing onespyenv
allows you to install (build from source) different versions of Python alongside each other; you can then clone them with virtualenv or use pyenv to select which one to run at any given timeLonger version:
Virtualenv allows you to create a custom Python installation e.g. in a subdirectory of your project. This is done by cloning from an existing Python installation somewhere on your system (some files are copied, some are reused/shared to save space). Each of your projects can thus have their own python
(or even several) under their respective virtualenv. It is perfectly fine for some/all virtualenvs to even have the same version of python
(e.g. 3.8.5) without conflict - they live separately and don't know about each other. If you want to use any of those python
s from shell, you have to activate
it (by running a script which will temporarily modify your PATH
to ensure that that virtualenv's bin/
directory comes first). From that point, calling python
(or pip
etc.) will invoke that virtualenv's version until you deactivate
it (which restores the PATH
). It is also possible to call into a virtualenv Python using its absolute path - this can be useful e.g. when invoking Python from a script.
Pyenv operates on a wider scale than virtualenv. It is used to install (build from source) arbitrary versions of Python (it holds a register of available versions). By default, they're all installed alongside each other under ~/.pyenv
, so they're "more global" than virtualenv. Then, it allows you to configure which version of Python to run when you use the python
command (without virtualenv). This can be done at a global level or, separately, per directory (by placing a .python-version
file in a directory). It's done by prepending pyenv's shim python
script to your PATH
(permanently, unlike in virtualenv) which then decides which "real" python
to invoke. You can even configure pyenv to call into one of your virtualenv pythons (by using the pyenv-virtualenv
plugin). You can also duplicate Python versions (by giving them different names) and let them diverge.
Using pyenv can be a convenient way of installing Python for subsequent virtualenv use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With