Problem: You want to maintain multiple different versions of python and keep packages separated based on projects that you're working on.
Solution: Use the pyenv, virtualenv tools along with the pyenv-virutalenv plugin to manage multiple versions of python and seamlessly integrate them with your projects' virtual environments.
All of these steps are specific for Mac OS X. For other operating systems, I've provided links to each tool's documentation.
You'll be installing the following things:
- pyenv + any number of python versions you want
- virtualenv
- pyenv-virtualenv
-
Install globally with pip (if you have pip 1.3 or greater installed globally):
sh $ [sudo] pip install virtualenv
-
Install pyenv using the Homebrew package manager for Mac OS X.
$ brew update $ brew install pyenv
To upgrade pyenv in the future, use
upgrade
instead ofinstall
. -
Add
pyenv init
to your shell to enable shims and autocompletion. Please make sureeval "$(pyenv init -)"
is placed toward the end of the shell configuration file since it manipulatesPATH
during the initialization.$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Zsh note: Modify your
~/.zshenv
file instead of~/.bash_profile
.Ubuntu and Fedora note: Modify your
~/.bashrc
file instead of~/.bash_profile
.General warning: There are some systems where the
BASH_ENV
variable is configured to point to.bashrc
. On such systems you should almost certainly put the abovementioned lineeval "$(pyenv init -)"
into.bash_profile
, and not into.bashrc
. Otherwise you may observe strange behaviour, such aspyenv
getting into an infinite loop. See #264 for details. -
Restart your shell so the path changes take effect. You can now begin using pyenv.
$ exec "$SHELL"
-
Install Python versions into
$(pyenv root)/versions
.For example, to download and install Python 2.7.8, run:
$ pyenv install 2.7.8
NOTE: If you need to pass configure option to build, please use
CONFIGURE_OPTS
environment variable.NOTE: If you want to use proxy to download, please use
http_proxy
andhttps_proxy
environment variable.NOTE: If you need to use Pyinstaller, you will need to install your Python versions with framework support enabled, like so:
$ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.5.0
NOTE: If you are having trouble installing a python version, please visit the wiki page about Common Build Problems or the FAQs section
-
Install pyenv-virtualenv using the Homebrew package manager for Mac OS X.
$ brew update $ brew install pyenv-virtualenv
To upgrade pyenv-virtualenv in the future, use
upgrade
instead ofinstall
. -
Add
pyenv virtualenv-init
to your shell to enable auto-activation of virtualenvs. This is enirely optional but pretty useful.sh $ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
Fish shell note: Add this to your
~/.config/fish/config.fish
sh status --is-interactive; and source (pyenv virtualenv-init -|psub)
Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.
Pyenv note: You may also need to add eval "$(pyenv init -)" to your profile if you haven't done so already.
-
Restart your shell to enable pyenv-virutalenv
sh $ exec "$SHELL"
-
Make sure the version of python you'll be using is installed
For example, to download and install Python 3.6.3, run:
$ pyenv install 3.6.3
-
Create virtualenv based on your required version of python
Provide the name and version of python to use. If the version is left blank, it will use your environment's current version. You can check your current version using
$ pyenv versions
.sh $ pyenv virtualenv 3.6.3 venv36
NOTE: You can list existing virtualenvs with
pyenv virtualenvs
NOTE: You can delete existing virtualenvs with
pyenv uninstall <virtualenv_name>
-
Set your project to always use this virtualenv.
sh $ pyenv local venv36
This will create a local file called
.python-version
. If you've set everything up correctly, this virtualenv should be activated upon entering this directory and deactivated when leaving.
This:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
in ~/.zshrc works!