In our daily work we usually deal with multiple python projects that we maintain. It’s usually the case that those projects have slightly different python versions. Some of the projects get the frequent updates, some of them might sit somewhere in your hard drive with old, but good python3.7 waiting for the developer to update the version.
The management of the python versions was always a hassle.
Especially when it comes to the virtualenvs.
In a typical scenario, when developer starts a project he/she creates a folder, and adds
an nested .venv
or venv
directory inside of the project folder, where the virtual
environment files will be stored. The python version, installed packages will all nest
in that folder.
After a while the list of the existing projects extends and the virtual environments go out fo control. Which python version is used for the project? Which packages are installed? Does the python version needs to be updated? In order to answer these questions developer would neeed to iterate over all of the folders and run corresponding commands in terminal.
To solve the python versioning hassle, virtual environment issues I highly recommend to use pyenv.
Key benefits:
- you can install any python version available in pypi at your machine
- time saving: you do not need to care about the installation process, pyevn will do everything for you
- manages virtualenvironments- it is a single tool to add manage both the python version as well as the virtualenvironments.
- allows to list all existing virtual environemnts and relate to corresponding projects
How to install
To install the main package run
brew install pyenv
and the the virtualenv manager
brew install pyenv-virtualenv
Add to ~/.bash_profile
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
The useful commands
List all available python versions
pyenv install --list
Install specific python version (the python will be installed in ~/.pyenv/versions)
pyenv install 3.5.5
Uninstall python version
pyenv uninstall 2.7.15
Show commands
pyenv commands
Show all installed versions
pyenv versions
Show current active version
pyenv version
Show the full path to the binary
pyenv which pip
Set local python version (creates .python-version)
pyenv local 3.4.5
VIrtualenv Create a virtualenv
pyenv virtualenv 3.5.5 project1
Activate an environment
pyenv virtualenv 3.5.5 project1
cd project1
# will activate the virtualenv (if you cd out of the project dir virtualenv will be different)
pyenv local project1
Activate the environment
pyenv activate venv
Deactivate the environment
pyenv deactivate venv
List the virtual environments
pyenv virtualenvs
To see which pip is used
pyenv which pip