Home » Python » Install Python on Mac OS

About Sergio Lauriano Junior

Sergio is graduated in Software Development in the University City of São Paulo (UNICID). During his career, he get involved in a large number of projects such as telecommunications, billing, data processing, health and financial services. Currently, he works in financial area using mainly Java and IBM technologies.

Install Python on Mac OS

In this article, we’re going to install Python on Mac OS using the software pyenv. Also, we’ll see some configurations to set our Python version and run an example.

1. Introduction

python mac

1.1 Pre-requisites

For this article, it’s recommended to install the package manager Homebrew. It’s an open-source package management system that makes simpler the installation of software on a macOS system.

To install it, access the website here and follow the instructions.

Also, feel free to use any IDE/Code Editor with support for Python language and versions recommended above.

2. Why do we use Python

Python is a programming language used to develop computer applications. Also, is one of the most used and studied technology in the world.

Know how to programming in Python is a great deal for developers as for other professionals since Python has applicable to many areas like data science, statistics, finances, games, and so many others.

In the next steps, we’re going to install Python on Mac OS using Homebrew. Further, we’ll know the software pyenv, used to handle different Python versions, and finally write our first application in Python.

3. Installing pyenv

Firstly, using Homebrew we’ll run the command below to install pyenv.Homebrew command install pyenv

$ brew install pyenv

And voilá pyenv is installed that easily. To confirm the installation, use the version command.pyenv version

$ pyenv --version
pyenv 1.2.22

4. Installing Python

Moving on, let’s install Python using pyenv. To do that, we’ll use the install command as follows:Installing Python with pyenv

$ pyenv install 3.9.1
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.9.1.tar.xz...
-> https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz
Installing Python-3.9.1...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.9.1 to /Users/slauriano/.pyenv/versions/3.9.1

Note: for this article, the most recent Python version available to install with pyenv was 3.9.1. You can check the versions available to install using the command install --list and find out what version fits your needs.

To check if the install was successful, use the command versions with pyenv.Check Python version with pyenv

$ pyenv versions
* system (set by /Users/slauriano/.pyenv/version)

5. Set your global default

Now, it’s time to set the main version of Python. That means, we’re going to choose which version of Python we’ll use as default.

To do that, we use the command global with pyenv.Set global default

$ pyenv global 3.9.1

Using the command versions on pyenv, we can check if the version was changed as desired.Set global default

$ pyenv versions    
* 3.9.1 (set by /Users/slauriano/.pyenv/shims/version)

5.1 Troubleshooting global default

If the global default version of Python set before wasn’t changed as required, you’ll need to set the init command to pyenv work properly.pyenv init command

$ eval "$(pyenv init -)"

You can also add this command to your ~/.profile (or ~/.bash_profile) to load into your terminal properties.

6. Write a program and run it

To write a program in Python you can use any text editor of your choice. I put the code below that just reverses the text typed by the user.Python reverse text code

#!/usr/bin/env python

def reverse(text):
    reverseText = text[::-1]

if __name__ == "__main__":
    text = input("Insert your text to reverse: ")

To run it, use the line as follows.Running python code

$ python -m reverse_text

And the result should be like thatRunning python code

Insert your text to reverse: Sergio

7. Understanding Python runtimes

Why is important to understand Python runtimes? When we use pyenv to set our Python version to execute the code, we need to be careful with the version we choose to be the global default.

In other words, the version defined as the global must fits with the Python’s code that we want to execute. There are some differences between Python 2 and Python 3 syntaxes, which can cause some failures during the software running.

So, always double-check your code to guarantee that you’re aligned with code and environment versions, preventing mismatch.

8. Conclusion

In this article, we learned how to install Python on Mac OS using the software pyenv, a great software to install and manage Python versions. Also, we saw the utility of Homebrew, a package manager designed for macOS to easily install third-party software.

Further, we could install pyenv and do some commands to install a Python version and set the global default to use the required Python runtime.

Finally, we wrote a simple Python code to run using our settled version and saw why is important to know the runtime to execute our code.

9. Download the source code

You can download the full source code of this example here: Install Python on Mac OS

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments