After recently upgrading my homebrew, links to python2 and pip2 broke!
I had no way to run different versions of Python.
This left me searching for a more effective way to manage Python versions and dependencies.
I discovered pyenv, a great tool for specifying exact Python versions. It also integrates nicely with virtual environments and Jupyter!
Setting Up Pyenv
- Install pyven
brew install pyenv - Setup
.bash_profileto configure and initialize pyenv# pyenv for running specific python version export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" # initialize pyenv and virtualenv in shell if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" fi # show virtualenv changes in prompt export PYENV_VIRTUALENV_DISABLE_PROMPT=1
Using Pyenv
- Install a specific python version
$ pyenv install 2.7.10 $ pyenv install 3.5.0 $ pyenv versions
You can similarly uninstall a specific version using uninstall.
pyenv introduces three scopes for using python versions. It uses the first found in order of specificity: shell, local, and global (else defaults to system python).
- Set the global Python version
$ pyenv global 2.7.10 - Set the local version
$ pyenv local 3.10 # to remove $ pyenv local --unsetThis creates a
.python-versionfile in the directory. Pyenv automatically activates the local python version for all subdirectories: - Shell overrides the python version for this shell session
$ pyenv shell 2.7.10 $ pyenv shell --unset
Virtual Environments
Install virtualenv plugin for pyenv.
Use Homebrew: brew install pyenv-virtualenv
Working with Virtual Environments
- Create a new virtual environment
$ pyenv virtualenv 2.7.10 my-project-2.7.10
This stores the virtualenv in a new directory my-project-2.7.10 under $(pyenv root)/versions/—similar to virtualenvwrapper.
- Activate or deactivate
$ pyenv activate <name> $ pyenv deactivate
Pyenv and Jupyter
How do you setup Jupyter kernels for Python 2 and Python 3?
- activate shell for each python version
pyenv activate 2.7.14 - Install Jupyter
pip install jupyter - Install Kernel
python -m ipykernel install --user
Et voila!