Contents
If you’re a Python programmer on a Mac, you may want to make Python 3 the default version when running Python scripts. Here’s how to do it.
Checkout this video:
Introduction
When you install Python 3 on your computer, it is typically installed as the default Python. However, if you have an older version of Python 2 installed on your computer, you can make Python 3 the default Python by following these steps.
How to make Python 3 default on Mac?
If you have multiple versions of Python on your system, you can select which one is the default by opening the Command Line and entering the following:
– `python – version` # Check whether Python 2 or 3 is installed
– `python3 – version` # Check whether Python 3 is installed
– `which python` # Check which version of Python is set as the default
– `alias python=python3` # Set Python 3 as the default
Why make Python 3 default on Mac?
Python 3 is the latest and most up-to-date version of the Python programming language. It’s recommended that you use Python 3 for all new projects, given that it has superior features to older versions of Python. You may need to make Python 3 the default on your Mac if you have multiple versions of Python installed and want to ensure that new projects use Python 3.
How to set Python 3 as the default version of Python?
To set Python 3 as the default version of Python on your Mac, open a Terminal window and run the following command:
export PATH=”/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin”
This command will ensure that Python 3 is used when you type “python” into the Terminal.
How to install Python 3 on Mac?
As of macOS 10.15, Apple has shipped Python 3 with the operating system. You can check which version of Python you have by entering the following command in the Terminal:
python – version
If the command returns “Python 3.7.4” or a later version, then you already have Python installed on your Mac.
How to check if Python 3 is installed on Mac?
To check if Python 3 is installed on Mac, open the Terminal app and type “python3 – version”. If you see something like “Python 3.7.2” or higher, then Python 3 is installed on your computer.
How to use Python 3 on Mac?
There are a number of ways to use Python 3 on a Mac. The recommended way is to use a Python 3 virtual environment. This will allow you to keep your system Python 2 version as the default and only switch to using Python 3 when you need it.
First, install virtualenv using pip:
pip install virtualenv
Then, create a new virtual environment for Python 3:
virtualenv -p python3 myvenv
This will create a new directory called myvenv that contains a copy of the Python 3 interpreter and all the necessary libraries. To use this virtual environment, you need to activate it:
source myvenv/bin/activate
How to uninstall Python 3 from Mac?
Python 3 is the default interpreter on Mac OS X, but if you have a different version of Python installed, you can make 3 the default by running the following commands in Terminal:
unlink /usr/local/bin/python
ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 /usr/local/bin/python
You can verify that this worked by running python – version in Terminal, which should now return Python 3.x.x.
FAQs
As of September 2016, the latest version of Python 3 is 3.6. If you installed multiple versions of Python on your system, e.g. Python 2 as well as Python 3, and you want to make Python 3 default, then there are various ways to do it. On a Mac system, the easiest way to set the default Python interpreter is to create an alias in ~/.bash_profile (or ~/.bashrc if that’s what your system is using).
Open the file in a text editor and add the following line:
alias python=python3
Save the file and close it. Then open a new Terminal window or type source ~/.bash_profile (or ~/.bashrc) to reload the file. Now when you type python in the Terminal, it will use Python 3 instead of 2.7.
Conclusion
As you can see, there are a few different ways to make Python 3 your default Python version on a Mac. While we recommend using the pyenv method, any of the other methods will also work just fine.