A Python setup that finally works
Vincent Thorne · Posted 29 Jun 2022
My punctual use of Python lead to a rocky situation on my computer: multiple installed versions of Python with some managed by Anaconda, unruly export PATH
s and multiple virtual environments. It (kind of) worked when I needed to, but I felt a complete lack of control. In short, my computer was the embodiment of xkcd’s famous comic:
That was until my colleague Matteo took the time the other day to teach me his own protocol to install and maintain a healthy Python ecosystem. This post will walk the reader through the steps that helped me achieve Python sanity on macOS.
The Steps
These commands are to be run in the terminal. They assume that you have Homebrew installed (which I recommend — some reasons why): install instructions are available here.
Controversial Step 0: if installed, remove Anaconda completely and clean your .zshrc
of all export PATH
s created by Anaconda or conda. More details on Anaconda’s help page. Not taking a stance for or against Anaconda/conda here, just advising to clean up old layers of Python before laying the sturdy foundations!
- Install Python from Homebrew
brew install python
- Make sure that the
export PATH
in.zshrc
is set to the one prescribed by Homebrew and that there are no other export pathsexport PATH="/usr/local/opt/python/libexec/bin:$PATH"
- Restart the terminal session
- Navigate to the directory of the project
cd path/to/project
- Install virtualenv with pip
pip install virtualenv
- Create a new virtual environment
virtualenv [environment-name]
- Activate (source) the new environment
source [environment-name]/bin/activate
- Install required packages with pip
pip install [package]
- If necessary later on, upgrade packages
pip install [packages] --upgrade
- Once done managing the environment, deactivate it
deactivate
Spyder setup
Accustomed to RStudio and other statistical IDEs, Spyder is a close enough cousin to ease my transition into Python. A few further steps are necessary to link it with the custom environment created above.
- Install
spyder-kernels
in the virtual environmentpip install spyder-kernels
- Depending on your version of Spyder, it might ask you to install a specific version. Try with the version displayed first
pip install spyder-kernels==2.3.0
, for example
- Depending on your version of Spyder, it might ask you to install a specific version. Try with the version displayed first
- In Spyder preferences
- Go to the Python Interpreter tab and select “Use the following Python interpreter”.
- Click on the file icon
- In the Finder prompt, navigate to your virtual environment, the bin folder and select the
pyhton
file. - Click OK and restart Spyder
- Check if it works by
import [package]
a package installed in your virtual environment using the IPython console in Spyder
Important: if you create a new virtual environment, you will have to make sure that it has spyder-kernels
installed (step 1) and re-link Spyder to that new environment (step 2).
Updating and sharing your environment
- Export a list of packages with their version to a text file
pip freeze > requirements.txt
- Update the packages listed in
requirements.txt
pip install --upgrade -r requirements.txt
- Send
requirements.txt
to someone, and they just need topip install -r requirements.txt
to install all the packages
If nothing works
Take a deep breath and ask your knowledgeable friends and colleagues to take a look at your computer and help you figure things out. There is light at the end of the Python tunnel!