Switch between Python 2.x 3.x versions in Mac Terminal


There are many ways in which you can install Python on a Mac, directly from Python.org using dmg files or using the brew package manager.

Most macOS comes bundled with the latest version of Python 3.x as well as Python 2.7 and you may have installed others for your projects.

If you are wondering how to easily access these different versions using a Mac terminal, then the best way is to create an alias for them.

Based on which Terminal Shell you use, open the profile file ~/.bashrc or ~/.zshrc and add the aliases for different Python versions you have.

Example:
# Alias for Python 2.7 
alias python2='/usr/bin/python2.7'

# Alias for Python 3.8
alias python3.11='/Library/Frameworks/Python.framework/Versions/3.8/bin/python3'

# Alias for Python 3.9
alias python3.11='/Library/Frameworks/Python.framework/Versions/3.9/bin/python3'

# Alias for Python 3.11
alias python3.11='/Library/Frameworks/Python.framework/Versions/3.11/bin/python3'

Save and refresh the prompt and now you can easily access any Python version by simply using the. version number.

Switch Python Versions using Mac Terminal Easily using Alias
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap