Step 1: Meet your default python
You are on Mac OS. When you run
python in your terminal you see this. You have Python
This is your default python. You can do things like add
Step 2: Meet your modules
From your interpreter you can see your available modules by asking for “help”
Note that you have a bunch of modules – including “antigravity” 🛸 Try it!
> import antigravity
But where are these modules?
For this investigation you need to import the “sys” module – which gives you access to the “system” – and print your “path” – which will print *most* of the folders that python will search for your modules.
> import sys
* Note: if you
ls into any of these folders, you will find a bunch of modules
NOTE: Python 2 was sunset on January 2020 so use Python version 3.x
How to install Python 3 on Mac
Homebrew is a great installer for Mac and its main job is to make it easier to install packages.
Step 1: Download Homebrew here
"brew" hosts its package information here as “formulae“.
$ brew install python --verbose
So what happens when I run brew install?
When you run the command, brew will look for the package in the “formulae” list and run the installation script for the package.
Brew installs *all* packages into its own directory at
Take a look with
$ ls /usr/local/Cellar.
After installation, brew creates a symlink which is basically a shortcut that will allow your system to run the new command
Wait, not all libraries use the latest version of python.
As of today,
tensorflow DOES NOT support version 3.7. Can I downgrade my python version with brew?
Well, not easily… if you already have the older version downloaded, you can run
brew switch python #.#.
But if you don’t have it downloaded, you’ll need to revert brew and do unrecommended stuff.
So how do I juggle versions of python?
The main job of
pyenv is to manage which version of python you prefer to run.
pyenv prepends a directory to your $PATH variable and hijacks every
pip command you run. Then it tries to find your “preferred” python version to run.
How does it know my preferred version?
pyenv will first check the
PYENV_VERSION environment variable (if set), then will move onto finding a
.python-version file in the current directory, then look in the parent directory, then that parents directory, all the way up to the root. If it doesn’t find that file, it will search for a
$(pyenv root)/version file and if it doesn’t find that, it will simply use your “system” python.
brew install pyenv
Step 2: add the
pyenv init command to your
.bash_profile with this command
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Step 3: run your bash profile to make sure the changes propagate
Now, if you run
python you will see that you are still on version 2.7.10 (or whatever your system python was from the start). This is because you have not yet set your preferred python anywhere else, and the search landed on your system python.
pyenv install --list to list all available python versions
Step 4: install a version of python that works with your favorite library
$ pyenv install 3.5.5
* I’m choosing 3.5.5 which I believe is the latest compatible with
Step 5: create a
.python-version file in your project
$ echo 3.5.5 > .python-version
Step 6: run python
You will eventually want to manage your package versions
Step 1: create a virtual environment in a directory we’ll call
env is arbitrary but a good convention – a folder called
env will be created where you run this command
$ virtualenv env -p `pyenv which python`
If you want to inherit from globally install packages, use this flag
$ virtualenv env --system-site-packages
Step 2: activate your environment
$ source env/bin/activate
Now, anytime you run
pip install <anything> all of your packages will be downloaded into your new
env folder without being installed in your global python
Step 3: pip install to your hearts desire
pip install --upgrade tensorflow