Upgrading and Using Python 3 on a Mac

Spread the love

You might think that your brand-new Mac has the newest version of all the relevant software. For most user apps, you’d be right, but for underlying frameworks it’s a different story. New Macs still ship with Python 2.7.10, even though the most recent stable release is Python 3.5. If this seems like a large gap between versions, that’s because it is. But the newer version isn’t necessarily better: Python 3 isn’t backwards compatible with Python 2, and most developers are still using Python 2.

Also read: 10 Useful Python One-Liners You Must Know

2to3 or not 2to3?

A naive user might think that Python 3 is better because it’s newer. They wouldn’t be totally wrong since Python 3 includes some cool new features that Python 2 does not have. However, alongside those new features, Python 3 also has a problem: it’s not backwards compatible with Python 2. That means that programs written for a Python 2 interpreter won’t run on a Python 3 interpreter. Even fundamental functions like print work differently between Python 2 and 3, making it non-trivial to move from one platform to another.

But why hasn’t the whole world switched over to Python 3? The main problem is that there’s a lack of compelling motivation. Python 2 is a strong language, and only power users will get to experience the full benefit of Python 3’s new features. Plus, Python 2.7.10 is pre-installed on all Macs and a number of Linux distros.

However, Python 2 isn’t going to be around forever. The developers have set 2020 as the final year for Python 2 support, and everyone will need to transition their programs over to Python 3 by then. Utilities like 2to3 make it easier to transform a Python 2 program into valid Python 3 syntax, but if you’ve ever used Google Translate, you know this won’t be perfect.

Upgrading to Python 3 on your Mac

Even if it isn’t the de facto standard, you can run Python 3 on your computer today. You can even run it alongside a concurrent Python 2.7 installation without affecting the version 2.7 installation.

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

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

3. If you open the Applications folder, you’ll find a new Python 3.x 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.

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.

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

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

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.)

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.

3. Chose “File > Open…” from the menu bar and select your Python script.

4. The script will now run in a Terminal window.

Conclusion

Installing Python 3 on a Mac isn’t hard. The challenge here is changing your own coding habits. If you’ve been writing Python 2 for a while now, switching to Python 3 might feel like turning around a cruise ship. But you’ll have to learn to do it eventually, so you may as well get started while you have a few years ahead of you.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Alexander Fox

Alexander Fox is a tech and science writer based in Philadelphia, PA with one cat, three Macs and more USB cables than he could ever use.

Comments are closed