Install Python 3 on macOS

Oct 27, 2018 4 min read
Install Python 3 on macOS

Motivation

We want to run and develop software on our mac, for this we need to install Python.

Method 1: Using Homebrew

I use this one so I can easily upgrade all my environment with a simple command in homebrew.

brew install python

Method 2: Direct download

1. Download the most recent package from the Python website.

python-3-download-page

2. Double-click on the downloaded file to run the Python 3 installer.

python-3-installer

3. If you open the Applications folder, you’ll find a new Python 3.x folder.

python-3-applicaitons-folder

4. Inside that folder you’ll find a GUI interface for launching Python applications, as well as IDLE, an IDE for developing Python applications.

python-3-folder

Running Python 3

There are a couple ways that you can run Python 3 scripts on your Mac.

1. To run Python 3 from the Terminal, you’ll use the command python3. This is different from the python command which will load up Python 2.7.

python-3-cli

2. That command, without any additional arguments, will invoke the Python 3 interactive interpreter.

python-3-cli-2

3. If you want to run a script with the Python 3 interpreter, follow the python3command with the path to your .py file.

python-3-cli-3

You can also run Python 3 programs from the Python Launcher GUI. For running a quick script from Terminal, using the Launcher doesn’t have any advantages, but if you want to set flags and options, this might be an easier way to go about it.

1. Open the Python Launcher found in “/Applications/Python 3.5.” (Note that the number in the Python folder may change with future versions.)

python-3-launcher

2. This will open a Preferences window. By default, the launcher will run everything with a Python 2 interpreter. To change this to Python 3, you’ll need to change the directory path under “Interpreter” to /usr/local/bin/python3. That is where the Python 3.5 interpreter is installed by default.

python-3-gui-preferences
python-3-interpreter-path

If you now type python --version, you'll notice that it says Python 2.7.6, that's because if you want to use Python 3, you must type: python3 --version

A simple safe way would be to use an alias:

For Python:
cd ~ && echo "python=python3.8" >> ~/.bashrc && echo "python=python3.8" >> ~/.bash_aliases && echo "python=python3.8" >> ~/.zshrc

For pip to pip3
cd ~ && echo "pip=pip3" >> ~/.bashrc && echo "pip=pip3" >> ~/.bash_aliases && echo "pip=pip3" >> ~/.zshrc

After adding the above in the file, run the command below to refresh:
source ~/.bashrc && source ~/.bash_aliases && ~/.zshrc

To disable the alias in the current shell use the unalias built-in command:
unalias python

Upgrade installs

For pip: python -m pip install --upgrade pip

Bonus: Virtual Environments

You should always use a virtual environment for your Python projects. A virtual environment is a way to create an isolated space so you can, for example, run Python 2.7 for one project and Python 3.7 for another on the same computer. We can use the built-in venv module for this.

It’s a best practice to keep all your virtualenvs in one place. Let’s create a folder:
mkdir ~/.virtualenvs

Now create a new virtual environment called project1_env by running:
virtualenv -p python3 project1_env or if you are on windows: virtualenv project1_env

Activate VEnv

MAC:
Because we used python3 here our virtual environment knows that when we type python for a command we mean Python 3 not Python 2. In order to activate this virtual environment, so we can use it, we must also run the following command:
source project1_env/bin/activate

Windows
Virtualenv creates a batch file in your new venv \env\Scripts\activate.bat, you can enter it on the command line or simply drag and drop it in the terminal and hit enter.

Note that while the environment is active, you will see its name in parentheses. Software packages we install now will only be available within this virtual environment.

To stop using a virtual environment, either close the Terminal window or enter deactivate.

Save requirements

You can use the command pip freeze --local > requirements.txt to create a text file with the installed packages of that specific enviroment.

Install packages from saved requirements

To install the requirements you saved on that text file on the same or different virtual env., simply type pip install -r requirements.txt

Great! Next, complete checkout for full access to ArturoFM.
Welcome back! You've successfully signed in.
You've successfully subscribed to ArturoFM.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.