IMPORTANT!
- pyenv manages multiple versions of Python
- pyenv-virtualenv manages virtual environments for different versions of Python
brew install pyenv
installs pyenvbrew install pyenv-virtualenv
installs pyenv-virtualenv -- a plugin that provides features to manage virtualenvspyenv install --list | grep " 3\.[678]"
lists all Cpython versionspyenv install --list | grep "jython"
lists all Jython versionspyenv install --list | grep "pypy"
lists all PyPy versionspyenv install 3.8.0
installs Python 3.8.0pyenv uninstall 3.8.0
uninstalls Python 3.8.0
pyenv versions
lists all installed versions on our machine. The*
indicates the currently active Python version. In the case below, we are using Python 3.8.0* 3.8.0 (set by /Users/cereblanco/.pyenv/version) 3.9.0
pyenv global 3.9.0
sets the active version to Python 3.9.0 globally on our machinepyenv local 3.8.0
sets the active version to Python 3.8.0 locally in the current directory. This is often used when we are working on a specific application-- it sets the active version locally in the project's root directory.pyenv shell 3.8-dev
sets the active version to Python 3.8-dev on the currently active shell/cli. It is deactivated once the shell is exited.
-
cd path-to-my-project/myproject
changes the directory to the given project -
pyenv virtualenv <python_version> <virtualenv_name>
creates a virtualenv named virtualenv_name with python_versionExample!
pyenv virtualenv 3.8.0 myproject
creates a virtualenv myproject using python 3.8.0 -
pyenv local myproject
activates locally the myproject environment in the current directory (myproject's root directory) -
pyenv activate myproject
activates myproject environment -
pyenv deactivate
deactivates the myproject environment -
pyenv local --unset
removes the myproject environment from the local environment
Tips!
- Setup a separate local environment for each project
- When working on a project, we activate the specific environment for that project
cd path-to-my-project/another_project
changes the directory to another specific application, for instance another_projectpyenv virtualenv 3.8.0 another_project
creates a virtualenv for another_projectpyenv local another_project
sets this another_project environment locally in the current directorypyenv activate another_project
activates the another_project environmentpyenv deactivate
deactivates theanother_project
environment
For instance, we want to use Python 3.9.0 and also wanted to check if the current project still works on the newest stable build Python 3.10 (Python 3.10-0a7)
cd path-to-my-project/myproject
let's continue working on myprojectpython --version
shows the currently active Python versionmyproject // python3.8
pyenv virtualenv 3.10.0a7 myproject-another-env
creates another environment myproject-another-env using Python 3.10.0a7pyenv local myproject myproject-another-env
sets the two environmentsmyproject
myproject-another-env
locallypyenv local
shows the environments in the current directoryIn our current myproject directory, we can directly use python.3.8 and python3.10 simultaneouslymyproject // python3.8 myproject-another-env // python3.10
$ python3.10 --version $ python3.8 --version
[1] https://realpython.com/intro-to-pyenv/ [2] https://github.com/pyenv/pyenv