Toggle Sidebar   Previous Lecture Complete and Continue  

  Install Python 3 and Virtual Environments

Let’s install a real version of Python.

It takes some configuration to properly install Python 3 on a Mac, Windows, Linux, or Chromebook computer and there are multiple approaches. Many developers follow the advice on the official Python website to download distinct versions of Python directly onto their computer and then adjust the PATH variable accordingly.

The problem with this approach is that updating the PATH variable correctly is tricky, by downloading Python directly updates are harder to maintain, and there are now much easier ways to install and start using Python quickly.

This lecture walks you through installing Python and how to set Python3 as a default python version on MacOS.

Install Xcode

Xcode is Apple's Integrated Development Environment (IDE). You might already have Xcode on your Mac. If not, you can get Xcode from Apple AppStore.

Install Python3 with Brew

By default MacOS ships with Python 2. But most of us have started to work with Python 3 and it is very irritating to run python3 every time instead of python in terminal. Here is how to do this.

Open the terminal (bash or zsh) whatever shell you are using.

Install python-3 using Homebrew (https://brew.sh).

brew install python

Set Python3 as a default python version on MacOS

Look where it is installed.

ls -l /usr/local/bin/python*

The output is something like this:

lrwxr-xr-x  1 davidtran  admin  24 Nov 23 23:54 /usr/local/bin/python -> /usr/local/bin/python3.8
lrwxr-xr-x  1 root       wheel  69 Feb 18  2020 /usr/local/bin/python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3
lrwxr-xr-x  1 root       wheel  76 Feb 18  2020 /usr/local/bin/python3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3-config
lrwxr-xr-x  1 root       wheel  71 Feb 18  2020 /usr/local/bin/python3.8 -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
lrwxr-xr-x  1 root       wheel  78 Feb 18  2020 /usr/local/bin/python3.8-config -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8-config

Change the default python symlink to the version you want to use from above.

Note that, we only need to choose the one that end with python3.*. Please avoid using the ones' that end with config or python3.*m or python3.*m-config.

Below command shows how it should be done:

ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python

Close the current terminal session or keep it that way and instead open a new terminal window (not tab). Run this:

python --version

You will get this:

Python 3.8.1

Virtual Environments

Virtual environments are an indispensable part of Python programming. This is important because by default software like Python and Django is installed in the same directory. This causes a problem when you want to work on multiple projects on the same computer. What if ProjectA uses Django 3.0 but ProjectB from last year is still on Django 1.0? Without virtual environments this becomes very difficult.

In this course, we will use Pipenv to manage virtual environments. Pipenv is similar to npm and yarn from the JavaScript/Node ecosystem: it creates a Pipfile containing software dependencies and a Pipfile.lock for ensuring deterministic builds. That means each and every time you download the software in a new virtual environment, you will have exactly the same configuration.

To install Pipenv we can use pip which Homebrew automatically installed for us alongside Python 3.

pip install pipenv