• Shopping Cart Shopping Cart
    0Shopping Cart
London App Developer
  • Courses
  • Consulting
  • Tutorials
  • About
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu
blog title with the python logo
macOS, Python

Installing Python on macOS using PyEnv for beginners

In this post I’ll explain how to install Python using PyEnv on a macOS machine.

This tutorial is also available in video form here:

What is PyEnv and why use it?

macOS comes with Python pre-installed.

However, the version installed is usually 2.7, which was deprecated on January 1st 2020.

So you might be thinking: Why not remove the old version (2.7) and replace it with a newer version like 3.9 using the traditional installation method?

This is not recommended because there may be some software on your macOS which requires Python 2.7 to run.

In other words, if you remove this version — things may break.

Also, if you use Python professionally, you’ll likely need to use specific versions for different projects, so it’s useful to be able to switch versions as needed.

Enter PyEnv

PyEnv is a useful open source tool for managing multiple Python versions on your machine.

Using this tool, you can take advantage of all the latest features of Python 3.9, meanwhile ensuring that software requiring older versions of Python (like 2.7) continue to work on your machine.

In this tutorial, I’ll explain:

  1. How to install and configure PyEnv
  2. How to install a specific version of Python on your machine
  3. How to choose which version of Python you want to use for a specific project

Installing Homebrew

We’ll install PyEnv using a tool called Homebrew.

If you haven’t used it before, Homebrew is a popular package manager for macOS.

To install it, copy the line below and paste it into your Terminal window:

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

This one-line command (taken from the brew.sh website) will download and run a script that will install Homebrew on your machine.

You’ll need to enter your administrator password and follow the on-screen instructions:

Screenshot of Terminal after pasting Brew.sh installation command showing password prompt.
Screenshot of Terminal after pasting Brew.sh installation command.

The installation can take a few minutes.

Once it’s done, you should see something like this:

Screenshot of brew installation stating "next steps" with a link to docs.brew.sh.
Brew installed successfully.

Installing PyEnv using Homebrew

Once Homebrew is installed, we can use it to install PyEnv.

This can be done by running the command below:

brew install pyenv
Screenshot that shows the PyEnv installation command -- brew install pyenv -- typed into the terminal.
Screenshot of typing the pyenv installation command

In-case you’re curious, I’ll translate the command below:

  • brew – this is the package manager we installed above
  • install – this is the command that tells (Home)brew that we want to install something
  • pyenv – this is the name of the package we want to install (Tip: you can find other packages by using the search on brew.sh)

Once installation is complete, you’ll see something like this:

Screenshot of output after PyEnv is installed.

Important! Take note of the two outputs highlighted above (they may be different on your machine).

Configure PyEnv

Now we need to configure PyEnv so it loads when we open our Terminal.

What you see may be different from the screenshot above, because the output depends on your Terminal configuration.

For example, instead of ~/.zshrc, your may see ~/.profile or ~/.bashrc.

What if you closed the window or don’t see any output?

Don’t worry, it happens… Simply open a new Terminal and run the below command to show it again:

pyenv init

Next we need to add the line starting with “eval” to the file path displayed after “# the following to”.

Start by copying the contents of the “eval” line to your clipboard (highlight it, right click and select Copy):

Line starting with eval which needs to be copied to clipboard.
Line which you need you need to copy

Then, type:

nano <path displayed>

For example, if you see what’s displayed in the screenshot above, you would type:

nano ~/.zshrc

This will open the file in the nano text editor, which will look like this:

Screenshot of the nano editor
Nano editor

If your file already contains text, then use the down arrow key to navigate to the end of the file (do not modify existing lines).

Paste the eval line into the file, like this:

Next, press Control + X, type Y and press Enter when prompted for the file name.

Now PyEnv is installed and configured, exit and reload the Terminal.

Why do we need to do this?

PyEnv is a tool that is used through the Terminal.

It works by telling the Terminal which Python installation should be used, based on certain criteria such as the default configured or certain files available in the directory you’re working from.

For PyEnv to be able to do this, we must run some setup code every time you open a Terminal instance.

By performing the steps above, we are configuring the Terminal to run this code for every new window.

Install Python with PyEnv

Next we can install Python using PyEnv.

To do this, run the following command:

pyenv install 3.9.1 

Tip: You can replace 3.9.1 with any available Python version listed on the Python website.

The installation can take a few minutes.

Once complete, the output should look something like this:

Screenshot showing Terminal when installing Python with pyenv.
Example of the pyenv install command.

Set default Python version

There are multiple ways to set a Python version using PyEnv.

Each one is explained in the official documentation.

In this tutorial, we’ll set it using the ~/.pyenv/version file.

To set this, type the following command:

echo 3.9.1 > ~/.pyenv/version

Tip: The 3.9.1 in the command above must correspond to an available installed version.

For example, the screenshot below shows how you would set the version to 3.8.2:

Finally, verify the correct version is installed by running the following:

python --version

The output should display the version set above (eg: 3.9.1).

That’s how to install, configure and use Python on macOS using PyEnv.

This tutorial is an excerpt from our course: Python for Beginners: Learn how to code properly in 2021.

Tags: homebrew, Python
Share this entry
  • Share on Facebook
  • Share on X
  • Share on WhatsApp
  • Share on Pinterest
  • Share on LinkedIn
  • Share on Tumblr
  • Share on Vk
  • Share on Reddit
  • Share by Mail
https://i0.wp.com/londonappdeveloper.com/wp-content/uploads/2021/03/Artboard-1.png?fit=574%2C446&ssl=1 446 574 mark https://londonappdeveloper.com/wp-content/uploads/2018/12/lon_website_logo-300x110.png mark2021-03-31 16:10:472021-04-08 14:46:32Installing Python on macOS using PyEnv for beginners
You might also like
Debugging a Dockerized Django app with VSCode
Beginner's Guide to Python - Lesson 03 - Simple Variables Beginner’s Guide to Python – Lesson 03 – Simple Variables
Beginner's Guide to Python - Lesson 07 - Lists and sets Beginner’s Guide to Python – Lesson 07 – Lists and sets
Beginner's Guide to Python - Lesson 01 - Getting Set Up Beginner’s Guide to Python – Lesson 01 – Getting Set Up
Beginner’s Guide to Python – Lesson 06 – Simple Functions Beginner’s Guide to Python – Lesson 06 – Simple Functions
Beginner’s Guide to Python – Lesson 10 – Packages Beginner’s Guide to Python – Lesson 10 – Install packages
Beginner's Guide to Python - Lesson 04 - Simple If Statements Beginner’s Guide to Python – Lesson 04 – Simple If Statements
Beginner’s Guide to Python – Lesson 9 – Virtual Environments Beginner’s Guide to Python – Lesson 09 – Virtual Environments
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

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

Pages

  • About
  • Consulting
  • Home
  • Tutorials

Categories

  • AI
  • Android
  • Android Studio
  • Angular
  • Aptana Studio
  • AWS
  • Bootstrap
  • Career advice
  • Celery
  • Databases
  • Deployment
  • DevOps
  • Django
  • Django REST Framework
  • Docker
  • Eclipse
  • Getting Help
  • Git
  • Git Bash
  • Git Flow
  • GitHub Actions
  • Google App Engine
  • IDE
  • Ionic Framework
  • iOS
  • JavaScript
  • Job Search
  • Links
  • Linux
  • Mac OS X
  • macOS
  • Node.JS
  • OpenAI
  • PGAdmin
  • Postgres
  • Productivity
  • Python
  • React
  • Salt Stack
  • Stack Overflow
  • Tools
  • Travis-CI
  • Tutorials
  • Ubuntu
  • Uncategorized
  • Vagrant
  • VSCode
  • WebSockets
  • Windows 10
  • WordPress

London App Developer Ltd,

71-75 Shelton Street, Covent Garden, London, United Kingdom, WC2H 9JQ

Company registration number: 09718346

© London App Developer - London App Developer Ltd, Company Registration Number: 09718346, VAT: GB224061842
  • Link to X
  • Link to Youtube
  • Link to Facebook
  • Link to Reddit
Link to: What’s the difference between the migrate and makemigrations command in Django? Link to: What’s the difference between the migrate and makemigrations command in Django? What’s the difference between the migrate and makemigrations command in...migrate versus makemigrations with a thinking emoji Link to: How to use Terraform via Docker Compose for Pros Link to: How to use Terraform via Docker Compose for Pros Blog title with AWS Terraform and Docker logosHow to use Terraform via Docker Compose for Pros
Scroll to top Scroll to top Scroll to top

This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.

OKLearn more

Cookie and Privacy Settings



How we use cookies

We may request cookies to be set on your device. We use cookies to let us know when you visit our websites, how you interact with us, to enrich your user experience, and to customize your relationship with our website.

Click on the different category headings to find out more. You can also change some of your preferences. Note that blocking some types of cookies may impact your experience on our websites and the services we are able to offer.

Essential Website Cookies

These cookies are strictly necessary to provide you with services available through our website and to use some of its features.

Because these cookies are strictly necessary to deliver the website, refusing them will have impact how our site functions. You always can block or delete cookies by changing your browser settings and force blocking all cookies on this website. But this will always prompt you to accept/refuse cookies when revisiting our site.

We fully respect if you want to refuse cookies but to avoid asking you again and again kindly allow us to store a cookie for that. You are free to opt out any time or opt in for other cookies to get a better experience. If you refuse cookies we will remove all set cookies in our domain.

We provide you with a list of stored cookies on your computer in our domain so you can check what we stored. Due to security reasons we are not able to show or modify cookies from other domains. You can check these in your browser security settings.

Google Analytics Cookies

These cookies collect information that is used either in aggregate form to help us understand how our website is being used or how effective our marketing campaigns are, or to help us customize our website and application for you in order to enhance your experience.

If you do not want that we track your visit to our site you can disable tracking in your browser here:

Other external services

We also use different external services like Google Webfonts, Google Maps, and external Video providers. Since these providers may collect personal data like your IP address we allow you to block them here. Please be aware that this might heavily reduce the functionality and appearance of our site. Changes will take effect once you reload the page.

Google Webfont Settings:

Google Map Settings:

Google reCaptcha Settings:

Vimeo and Youtube video embeds:

Other cookies

The following cookies are also needed - You can choose if you want to allow them:

Privacy Policy

You can read about our cookies and privacy settings in detail on our Privacy Policy Page.

Privacy Policy
Accept settingsHide notification only