Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --versionPython 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
- Open Terminal
cdnano ~/.bashrcalias python=python3(Add this line on top of .bashrc file)- Press
ctr+o(To save the file) - Press
Enter - Press
ctr+x(To exit the file) source ~/.bashrcOR. ~/.bashrc(To refresh the bashrc file)
python --versionPython 3.7.5
macos - Add to python path mac os x - Stack Overflow
terminal - How to (and should I) put a path to user-installed python ahead of system-installed python? - Ask Different
Python's path on MacOS
Python's path on MacOS
Videos
Modifications to sys.path only apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATH environment variable:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
Note that PATH is the system path for executables, which is completely separate.
**You can write the above in ~/.bash_profile and the source it using source ~/.bash_profile
On MAC OS you can simply find the location of python/python3 by using the command which python or which python3. (works for Linux too)
And it should give something like:
For python
/usr/local/bin/python
For python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
Export the path to your bash_profile
In your terminal type
sudo nano ~/.bash_profile
Enter your password and paste the following lines
PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.9/bin/python3"
export PYTHONPATH
Press control + x to exit, and press y for saving on being asked to save
Press `enter' to return to terminal window
Source it using the following command in terminal, run
source ~/.bash_profile
Path to python3 should be updated now!!
You can accomplish this by adding
export PATH="/path/to/python:"$PATH
to your ~/.zshrc (or ~/.bash_profile if you're not on Catalina).
I'd like to propose an alternative and suggest pyenv, available via Homebrew. You can set a specific Python version to be "global" (i.e. default everywhere) and/or "local" (i.e. using that version in a specific directory only).
E.g.:
brew install pyenv
pyenv global 2.7.16
cd ~/myProjects/MyProject
pyenv local 3.7.6
If you were to use Python in ~/myProjects/MyProject it will default to 3.7.6, and anywhere else 2.7.16:
cd ~/some/other/directory
python --version
>> Python 2.7.16
cd ~/myProjects/MyProject
python --version
>> Python 3.7.6
https://github.com/pyenv/pyenv#homebrew-on-macos
I installed Python 3.8 for macOS using the installer from python.org, not homebrew which is also installed on my Mac but rarely used. I think the first three lines below to my ~/.bash_profile was automatically added by the installer from python.org.
With those, any changes I make using pip3 seem to affect only the 3.8 whereas those made via pip seem to affect only the 2.7 (which I believe comes installed with macOS). Because I did not use homebrew in installing Python 3.8 (or 2.7), I assume it is not relevant for either version of Python in my case. Hope these help.
# Setting PATH for Python 3.8
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
# Setting PATH for Python 2.7
# I believe the two lines below were already in the file before the above were added
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
I moved from Windows to a MacBook yesterday (runs MacOS 14 Sonoma on Apple Silicon). I'm trying to get Python to work properly on my MacBook but I'm having a hard time figuring it out.
I'm being able to run Python from the terminal but it doesn't run from VSCode. From the terminal (ZSH) too, only python3 ran but not python. To get around that, I followed an online article and added a ~/.zshrc file. I added these two lines to it:
alias python="/usr/bin/env python3" alias pip="/usr/bin/env pip3"
I tried to install a Python package using pip but it hit me with a WARNING: The script normalizer is installed in '/Users/my_username/Library/Python/3.9/bin' which is not on PATH.
So till now I've understood that Python can be installed on Mac in 3 ways:
-
The default inbuilt Python that comes with Mac out of the box.
-
Python installed from the Python website via an installer.
-
Python installed via Homebrew.
I am not being able to figure out the paths where each of these methods install Python. Which method is better for installing? Homebrew or download from the website? And once I have Python using one of the options, how do I make sure that it is the one used over the system Python? If I override the system Python, will there be any issues? How do I get Python to run from VSCode?
My MacOS and command line knowledge is very rudimentary so please correct me if I've said something wrong.
I moved from Windows to a MacBook yesterday (runs MacOS 14 Sonoma on Apple Silicon). I'm trying to get Python to work properly on my MacBook but I'm having a hard time figuring it out.
I'm being able to run Python from the terminal but it doesn't run from VSCode. From the terminal (ZSH) too, only python3 ran but not python. To get around that, I followed an online article and added a ~/.zshrc file. I added these two lines to it:
alias python="/usr/bin/env python3" alias pip="/usr/bin/env pip3"
I tried to install a Python package using pip but it hit me with a WARNING: The script normalizer is installed in '/Users/my_username/Library/Python/3.9/bin' which is not on PATH.
So till now I've understood that Python can be installed on Mac in 3 ways:
-
The default inbuilt Python that comes with Mac out of the box.
-
Python installed from the Python website via an installer.
-
Python installed via Homebrew.
I am not being able to figure out the paths where each of these methods install Python. Which method is better for installing? Homebrew or download from the website? And once I have Python using one of the options, how do I make sure that it is the one used over the system Python? If I override the system Python, will there be any issues? How do I get Python to run from VSCode?
My MacOS and command line knowledge is very rudimentary so please correct me if I've said something wrong.