I don't want to change system python and have the freedom to run python2 or python3 scripts with all needed modules. At the moment virtualenv seems the solution for me.
Follow instructions from [https://brew.sh/]
When homebrew work we can install python from brew:
# brew install python python3
During installation some handy stuff is printed, like adding the python path to $PATH.
Python2
# pip2 install --upgrade pip setuptools
And python3
# pip3 install --upgrade pip setuptools wheel
Install virtualenv and wrapper in the python2 environment
# pip2 install virtualenv virtualenvwrapper
Create virtualenv directory
# mkdir $HOME/.virtualenvs
Add the lines below to .bashrc or some other file which is being sourced
VIRTUALENVWRAPPER_PYTHON=$(which python2)
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
# uncomment following lines to load a virtualenv when opening a shell
#if (tty -s); then
# workon py3
#fi
# source .bashrc
Let's just start by creating a new python3 environment
# mkvirtualenv -p python3 py3
By adding some configuration we can make it harder to install modules in the global environment. Add the following to your pip confguration $HOME/Library/Application\ Support/pip/pip.conf
[install]
require-virtualenv = true
[uninstall]
require-virtualenv = true
The following functions could be used to change the global environment when needed:
gpip2(){
PIP_REQUIRE_VIRTUALENV="" pip2 "$@"
}
gpip3(){
PIP_REQUIRE_VIRTUALENV="" pip3 "$@"
}