Codementor Events

Getting started with Python 3 on MacOS

Published Mar 25, 2020
Getting started with Python 3 on MacOS

Coding in python is fun, but if you do not get it right from the start, it might get frustrating when you begin to run into issues with Python interpreters. In MacOS, Homebrew is the default software package manager. It's very easy to do your software installations with a simple command like

$ brew install <my_software>

While Homebrew is the best way to install some softwares in MacOS, it doesn't give the best experience for some softwares, including Python.

For me (like most developers), I didn't get it right from the start; I had everything messed up and learnt it the hard way. At some points my project would start breaking with some weired errors. When I had wasted a whole lot of time debugging and searching the internet, it would turn out to be issues with Python installation. Things got worse when I began working on multiple Python projects in the same machine. At this point, man had to get this thing right, and here is how to do it right.

## So, Do This and You'll be OK...

We are simply going to use pyenv to manage our Python environment. It simply helps you to manage multiple Python versions.

First, install pyenv using Homebrew.

$ brew install pyenv

Then, with pyenv, let's install our Python version of choice (say 3.8.0)

$ pyenv install 3.8.0

Now Add pyenv init to your shell to enable shims and autocompletion. Please make sure eval "$(pyenv init -)" is placed toward the end of the shell configuration file since it manipulates PATH during the initialization. You can do this by simply opening your shell file (.zshrc, .bashrc, .bash_profile or whatever you're using) and adding

eval "$(pyenv init -)" 

You can also run this command to achieve the same goal.

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

The above ensures you have pyenv working before initializing it.

To make our installed python version the default, run

$ pyenv global 3.8.0

To confirm you have the right version, run

$ pyenv version

I recommend you read this article by Matthew, https://opensource.com/article/19/5/python-3-default-mac. He has included many ways we do this wrong.
For more detailed pyenv commands, see https://realpython.com/intro-to-pyenv/.

Discover and read more posts from Obinna Nnenanya
get started
post commentsBe the first to share your opinion
Show more replies