This document will help you set up python for development on MacOS.
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
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.
Apart from the python version we need to create python environments each one specifying the versions for each package, we can use Virtualenvwrapper to do that. Virtual environments are useful becase we really don't want to have all the packages installed for all the different projects we are working on. Instead we isolate enviroments for each project.
On MacOs we run
brew install pyenv-virtualenvwrapper
And then we append to ~/.bash_profile
or ~/.zshrc
to
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"
export WORKON_HOME=~/.virtualenvs
pyenv virtualenvwrapper_lazy
Now all of our new environments will be created at ~/.virtualenvs
. Some useful commands to work with virutualenvs
# create a new virtualenv called my_environment
mkvirtualenv my_environment
# disable virtualenv
deactivate
# list all environments
workon
# reactivate virtualenv
workon my_environment
# remove virtualenv
rmvirtualenv my_environment
In the first command you create a new environment called my_environment
. You can find the interpreter in ~/.virtualenvs/my_environment/bin/python
. If you use, for instance, PyCharm you only need to select this project interpreter in the preferences tab. In most of the projects, once you activate the environment you need to install all the requirements of the project (set in a file call requirements.txt) as pip install -r requirements.txt
. Then you are set to develop your code!. Remember to add all the required packages in the file requirements.txt so that when you share the code with others I'll be almost plug and play.
Let's say we already have a jupyter notebook on our system (mine is at /usr/local/bin/jupyter
). Then, within your virtual envionment install ipykernel
pip install ipykernel
pip install ipython
pip install jupyter
pip install notebook
Yes, it would me more conveninent to add all this to your ´´´requirements.txt´´´ file.
And add the kernel to your jupyter
$(which python) -m ipykernel install --name=myenv
#ipython kernel install --user --name=myenv
You should be able to see the config running
cat /usr/local/share/jupyter/kernels/myenv/kernel.json
Now simply start a notebook and select your new kernel
jupyter notebook
To check which kernels do you have run
jupyter kernelspec list
And uninstall the recently created kernel as
jupyter kernelspec uninstall myenv
WARNING on Jupyter: If you want to add new packages doing pip install new_package
then you should install again the kernel running the same command as before $(which python) -m ipykernel install --name=myenv
when you are on myenv
in the terminal.