Ahmad Awais

NAVIGATE


SHARE


QuickTip: How to set Python default version to 3.x on macOS?

Ahmad AwaisAhmad Awais

If you are a macOS user like me, you know the default version of Python that comes by default with your mac is version 2.X. Most of the time, you’ll end up needing Python 3.X โ€” which I’m told you can run using the python3 command. But let’s set the default version to Python 3 โ€” so you can run it using the default python command.

Step #1: Install Homebrew#

Homebrew is an excellent package manager for macOS. Install it if you haven’t already by running the following command:

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

Step #2: Install Python with brew#

The simplest way to get this working is to install Python via brew. Run the following command in your terminal to install Python.

brew install python

It will install Python 3.

Now let’s create the right set of symlinks for Python 3.X so that it becomes the default python command in your macOS. Run the following command:

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

This should output something like the following:

/usr/local/bin/python -> /usr/local/bin/python3
/usr/local/bin/python3 -> ../Cellar/[email protected]/3.9.2_1/bin/python3
/usr/local/bin/python3-config -> ../Cellar/[email protected]/3.9.2_1/bin/python3-config
/usr/local/bin/python3.9 -> ../Cellar/[email protected]/3.9.2_1/bin/python3.9
/usr/local/bin/python3.9-config -> ../Cellar/[email protected]/3.9.2_1/bin/python3.9-config

Take a look at the first line. It shows default python being symlinked to the brew installed python3. If you don’t see that in the output, then we can be sure to set it as the default python symlink run the following:

ln -s -f /usr/local/bin/python3 /usr/local/bin/python
And we are all set.

Step #4: Verify python 3.X install#

To make sure we did everything right, let’s reload the shell/terminal โ€” you can do that manually or by running the following command to trigger a reload:

exec $SHELL -l
Tip: I have set an alias for this command to reload the shell, alias reload="exec $SHELL -l" which comes in handy all the time.

Now run the following:

which python

and you should see this output:

/usr/local/bin/python

โ€ฆ and finally, you can run the version check command:

python --version

All this should confirm Python 3.X install, as it outputs Python 3.9.2 for me. Easy peasy lemon squeezy. Use your code for good.

Founder & CEO at Langbase.com โ€” Ship composable hyper-personalized Al apps in seconds ยท Ex VP DevRel Eng. Rapid ยท Google Developers Advisory Board (gDAB) founding member. ๐Ÿง‘โ€๐Ÿ’ป AI/ML/DevTools Angel Investor โฏ AI/ML Advisory Board member San Francisco, DevNetwork

๐ŸŽฉ Award-winning Open Source Engineer & Dev Advocate ๐ŸฆŠ Google Developers Expert Web DevRel ๐Ÿš€ NASA Mars Ingenuity Helicopter mission code contributor ๐Ÿ† 8th GitHub Stars Award recipient with 5x GitHub Stars Award (Listed as GitHub's #1 JavaScript trending developer).

๐ŸŒณ Node.js foundation Community Committee Outreach Lead, Member Linux Foundation, OpenAPI Business Governing Board, and DigitalOcean Navigator. ๐Ÿ“Ÿ Teaching thousands of developers Node.js CLI Automation (100 videos ยท 22 Projects) & VSCode.pro course. Over 142 million views, 24 yrs Blogging, 108K developers learning, 200+ FOSS.

โœŒ๏ธ Author of various open-source dev-tools and software libraries utilized by millions of developers worldwide โ“ฆ WordPress Core Developer ๐Ÿ“ฃ TEDx Speaker with 100+ international talks.

โœจ As quoted by: Satya Nadella ยท CEO of Microsoft โ€” Awais is an awesome example for developers.
๐Ÿ™Œ Leading developers and publishing technical content for over a decade ๐Ÿ’œ Loves his wife (Maedah) โฏ Read more about Ahmad Awais.

๐Ÿ‘‹โ€ฆ Awais is mostly active on ๐• @MrAhmadAwais

๐Ÿ“จ

Developers Takeaway

Stay ahead in the web dev community with Ahmad's expert insights on open-source, developer relations, dev-tools, and side-hustles. Insider-email-only-content. Don't miss out - subscirbe for a dose of professional advice and a dash of humor. No spam, pinky-promise!

โœจ 172,438 Developers Already Subscribed
Comments 0
There are currently no comments.