The Random Thoughts of a Geek heading for Banbury
The Random Thoughts of a Geek heading for Banbury

Python development on macOS

Tamarisk helped me set this all up, she is a professional Python developer!

Make sure your mac is up to date and running the latest version of macOS, I am running macOS Monterey 12.0.1 at the moment.

Open a terminal and run the following command to install the Xcode Command Line Tools, this is required to allow everything else in this process to work:

sudo xcode-select --install

Everything else is installed using the amazing Brew package manager,

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After we have installed Brew, it is time to install python and pipenv

brew install python
pip install --user pipenv

Because I use zsh as my shell I have the following added to my ~.zprofile

export PATH="/usr/local/opt/python/libexec/bin:$PATH"
export PATH="$PATH:$(python3 -m site --user-base)/bin"
eval "$(_PIPENV_COMPLETE=zsh_source pipenv)"

The last line of this is tab completion for pipenv.

To check it was all working, we used this exercise from the hitchhiker’s guide to python as a test

mkdir ~/Desktop/test 
cd ~/Desktop/test 
pipenv install requests

In the Desktop/test folder we created a main.py with the following contents:

import requests
response = requests.get('https://httpbin.org/ip')
print('Your IP is {0}'.format(response.json()['origin']))

Running the script using pipenv run:

pipenv run python main.py

The output was

Your IP is 86.5.161.83.

Now hopefully this means that I can work on Python projects in folders without them causing issues with either System Python or other projects.

Leave a comment

Your email address will not be published. Required fields are marked *