Sitemap
Upendar Gareri

Upendar Gareri

Always from the <3. Software Developer by Day. Reader and Writer by Night. ❤ Storytelling. ❤ Table Tennis. Afraid of stagnation. List Maker. IND || DE.

Python setup on macOS

3 min readNov 14, 2021

--

Tools Used: Homebrew, Pyenv and Pipenv

On a Mac, the operating system needs the version of Python kept at /usr/bin/python*. You cannot do much to this version — and don’t try or you’ll mess up your Mac.

You can already use python3 as it is system installed but if you need to manage different versions of Python for various different projects then this guide can help you.

1. Install homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2. Install Pyenv

Pyenv is a python version manager.

brew install pyenv

Install Python using pyenv

pyenv install 3.10.0

The above command will install python 3.10.0.

Setup PATH for macOS

Edit your ~/.bash_profile and/or ~/.zshenv file so that the PATH comes before “/usr/local/bin” and “/usr/bin” where others install programs:

For .zshrc -

echo 'export PYENV_ROOT=$(pyenv root)"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.zshrc

Replace with .bashrc or so for others

Add pyenv init to your terminal

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc

Restart your terminal

exec "$SHELL"

Confirmation

which python

should return ‘ /Users/<name>/.pyenv/shims/python’

To know more about shims, read THEORY below

To check all python versions installed using pyenv

pyenv versions  # to check all python versions installed using pyenv

SET PYTHON VERSION

pyenv global 3.10.0  # set specific version as the default

If a particular project needs a different python version, then install that version first (if not installed already), navigate to the project folder, and set that version as local to that folder. Example -

pyenv install 3.6.5cd <path to project folder>pyenv local 3.6.5

This way, a specific project will use version 3.6.5, and all other projects by default will use the 3.10.0 version.

ORDER OF PATH OVERRIDE

When a shim is executed, it calls pyenv which loads Python release based on the following order:

1. $PYENV_VERSION environment variable

2. pyenv local x.y.z

3. pyenv global x.y.z

This means the PYENV_VERSION environment variable overrides all other specifications.

TROUBLESHOOT

If `python3 -V` doesn’t show 3.10.0 then it might be related to PATH

Get Upendar Gareri’s stories in your inbox

Join Medium for free to get updates from this writer.

From stackoverflow:

“Check your PATH. Somehow I have .pyenv/bin, when it should be .pyenv/shims. Fix the path with

export PATH=”$HOME/.pyenv/shims:$PATH”

(and add to zshrc etc)”

3. Install Pipenv

brew install pipenv

Navigate to the project folder and run

pipenv shell

This will create virtual environment with the same name as the folder name (plus some hash).

To install python packages from requirements.txt or Pipfile already in the project folder

pipenv install

To install additonal packages

pipenv install <package_name>

Pipenv Cheatsheet: https://upengareri.github.io/pipenv_cheatsheet/

THEORY

To get detailed understanding of how PATH works in macOS follow the below link — https://github.com/pyenv/pyenv

When you run a command like python or pip, your operating system searches through a list of directories to find an executable file with that name. This list of directories lives in an environment variable called PATH, with each directory in the list separated by a colon:

/usr/local/bin:/usr/bin:/bin

Directories in PATH are searched from left to right, so a matching executable in a directory at the beginning of the list takes precedence over another one at the end. In this example, the /usr/local/bindirectory will be searched first, then /usr/bin, then /bin.

pyenv works by inserting a directory of shims at the front of your PATH:

$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin

Through a process called rehashing, pyenv maintains shims in that directory to match every Python command across every installed version of Python — python, pip, and so on.

Shims are lightweight executables that simply pass your command along to pyenv.

That’s it :)

--

--

Upendar Gareri
Upendar Gareri