We'll use pyenv to manage python versioning and venv (comes with python 3) to manage virtual environments.
Pyenv allows you to have several python versions in your machine.
brew update
brew install zlib
brew install tcl-tk #otherwise we may have problems with tf package
brew install pyenv
And make sure you add the following to ~/.bash_profile
or to ~/.zshrc
# For compilers to find zlib you may need to set:
export LDFLAGS="${LDFLAGS} -L/usr/local/opt/zlib/lib"
export CPPFLAGS="${CPPFLAGS} -I/usr/local/opt/zlib/include"
# For pkg-config to find zlib you may need to set:
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH} /usr/local/opt/zlib/lib/pkgconfig"
Add also the lines to ~/.bashrc
or ~/.zshrc
# Pyenv
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
# this export is needed to set up the global and local python versions
export PATH="$PYENV_ROOT/shims:$PATH"
This installation should build the folder $HOME/.pyenv
. You can now list all the availiable python versions to download
pyenv install -l
Let's install for instance Python 3.1
pyenv install 3.1
Now your python interpreter of Python 3.1 is located in $HOME/.pyenv/versions/3.1/bin/python
so you could execute a python script written in 3.1 as
~/.pyenv/versions/3.1/bin/python MyScriptIn31.py
You can set a global python interpreter for the whole system, for instance I choose the latest availiable as of now
pyenv install 3.7.2 #install required version
pyenv global 3.7.2 #make it availiable though all the system
source ~/.zshrc # to refresh the terminal by default would be source ~/.bash_profile
Now every time we type python
in our shell, it will appear Python version 3.7.2. Thy this
python --version
which python
Now imagine we want to work on a specific python version (say 3.1) in a certain folder, we could go to the folder and type
pyenv local 3.1
It creates a file named .python-version
inside. Now if you check python --version
you'll see that is 3.1 whereas gobally is 3.7.2.
Now you just need to go to your project and create a specific python environment. First set up a local python version as described above and then create an environment called e.g. venv as follows
python -m venv ./env
source env/bin/activate
You are in your new environment. If you type
which python
it will appear the path_to_project/env/bin/python
. This is good! Our python interpreter is inside our project folder!. To deactivate the environment you just have to type
deactivate
And to remove the virtual environment just delete the folder venv
you just created.
Compared to pyenv and pyenvvirtualenvwrapper approach, this one is bound to local repos in the sense that you have to drive until the repo folder to activate it. Conversely in virtualenvwrapper the virtual environment is availiable systemwide. Also, a good thing of this approach is that sometimes virtualenvwrapper is a bit difficult to set up (in my experience).