When to handle multiple versions of python
Each python projects can require different versions of pythons such as python 2.7, python 3.7 or python 3.8. When developing on multiple projects, we need to be able to switch between different versions of python
- Python has “pyenv”
- https://github.com/pyenv/pyenv
- JavaScript has “nvm”, node version manager
- Ruby has “rvm” or “rbenv”
1. How to install pyenv
On mac
- Install with homebrew
brew install pyenv
- add the following block to your
.zshrc
or.bashrc
file
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
2. Install different python versions
➜ ~ pyenv
pyenv 1.2.21
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
--version Display the version of pyenv
commands List all available pyenv commands
exec Run an executable with the selected Python version
global Set or show the global Python version(s)
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version(s)
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
version Show the current Python version(s) and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
➜ ~ pyenv version
system (set by /Users/mkang/.pyenv/version)
➜ ~ python --version
Python 2.7.16
➜ ~ pyenv install 3.8.5
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.5.tar.xz...
-> https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
Installing Python-3.8.5...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.5 to /Users/mkang/.pyenv/versions/3.8.5
➜ ~ pyenv versions
* system (set by /Users/mkang/.pyenv/version)
3.8.5
➜ ~ pyenv global 3.8.5
➜ ~ pyenv versions
system
* 3.8.5 (set by /Users/mkang/.pyenv/version)
➜ ~ python --version
Python 3.8.5
➜ ~ pyenv install 3.7.5
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.5.tar.xz...
-> https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tar.xz
Installing Python-3.7.5...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.5 to /Users/mkang/.pyenv/versions/3.7.5
➜ ~ pyenv versions
system
3.7.5
* 3.8.5 (set by /Users/mkang/.pyenv/version)
➜ ~ python --version
Python 3.8.5
➜ ~ pyenv global 3.7.5
➜ ~ pyenv versions
system
* 3.7.5 (set by /Users/mkang/.pyenv/version)
3.8.5
➜ ~ python --version
Python 3.7.5