Skip to content

Instantly share code, notes, and snippets.

@adamscharf
Last active March 31, 2024 04:26
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save adamscharf/85a363f0bab254db00ccb3ea9ee45c72 to your computer and use it in GitHub Desktop.
Save adamscharf/85a363f0bab254db00ccb3ea9ee45c72 to your computer and use it in GitHub Desktop.
Managing Python using pyenv, virtualenv, and pyenv-virtualenv

Managing Python using pyenv, virtualenv, and pyenv-virtualenv

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.

Installation

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

virtualenv

Docs

  1. Install globally with pip (if you have pip 1.3 or greater installed globally):

    sh $ [sudo] pip install virtualenv

pyenv

Docs

  1. 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 of install.

  2. Add pyenv init to your shell to enable shims and autocompletion. Please make sure eval "$(pyenv init -)" is placed toward the end of the shell configuration file since it manipulates PATH 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 line eval "$(pyenv init -)" into .bash_profile, and not into .bashrc. Otherwise you may observe strange behaviour, such as pyenv getting into an infinite loop. See #264 for details.

  3. Restart your shell so the path changes take effect. You can now begin using pyenv.

    $ exec "$SHELL"
  4. 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 and https_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

pyenv-virtualenv

Docs

  1. 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 of install.

  2. 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.

  3. Restart your shell to enable pyenv-virutalenv

    sh $ exec "$SHELL"

Usage

Create project based virtualenv and automatically activate/deactivate

  1. 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
  2. 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>

  3. 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.

@StephenLeeUSTC
Copy link

Thank you for the awesome tutorial, switched to pyenv + virtualenv to manage my python and finally got a good night's sleep.

@adamscharf
Copy link
Author

adamscharf commented Jul 3, 2022

@StephenLeeUSTC Glad you found it helpful! Honestly, I typed this up for myself so that I didn’t have to keep figuring it out every time I setup a new machine.

@OleksVV
Copy link

OleksVV commented Aug 13, 2022

Dear @adamscharf
I am a (total) beginner. Please, help me.
I tried to run pyenv init and got
" # Load pyenv automatically by appending

the following to

~/.zprofile (for login shells)
and ~/.zshrc (for interactive shells) :
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Restart your shell for the changes to take effect."

Added all those strings to ~/.zprofile and ~/.zshrc, restarted my mac, but running pyenv init with the same result.
Tried to find a solution everywhere - no success.
Please, help me
(macOS Monterey, 12.5)

@OleksVV
Copy link

OleksVV commented Aug 13, 2022

additionally:
pyenv install 3.8.2
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.2.tar.xz...
-> https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
Installing Python-3.8.2...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 12.5 using python-build 20180424)

@zarifaziz
Copy link

zarifaziz commented Jun 24, 2023

Thanks for the great tutorial. One step that I was missing was how to actually activate the environment if auto-activation isn't configured.

So to activate your env:

. ~/.pyenv/versions/venv36/bin/activate

@JacobHagberg
Copy link

Thanks for the great tutorial. One step that I was missing was how to actually activate the environment if auto-activation isn't configured.

So to activate your env:

. ~/.pyenv/versions/venv36/bin/activate

Thanks, I was trying to figure this out

@Faizal-Patel
Copy link

This:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

in ~/.zshrc works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment