Mac OS X has installed python by default, but the version is 2.7.10. It is a little older, so I upgrade python to a newer version ( python 3.7 or python 3.9 ). But after installing the new version, when I open a terminal and run python
command, it still uses Python 2.7. This article will tell you how to fix it.
1. Install Python 3.7 & Python 3.9.
- Go to python download page and click the related link to download.
- After download, double click the installer to install it follow the wizard ( you can refer Install Python Linux And MacOS Version Tutorial ).
- When the installation process completes successfully. Open a Finder, click Go —> Go to Folder… menu item at the top menu bar.
- Input /Library/Frameworks/Python.framework/Versions in above Go to Folder dialog, click Go button.
- Now you can see all the 3 python version has been installed in the /Library/Frameworks/Python.framework/Versions folder.
2. How To Fix Installed Python3 But Still Python 2.7 Is Used Error.
- Now when you execute the
python
command in a terminal, you will find it still uses the old version which is 2.7.$ python Python 2.7.10 (default, Oct6 2017, 22:29:07)
- To use the new Python version, you need to run
python3
in the terminal.$ python3 Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:10:52) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
- To exit the Python interactive interpreter, input command exit(). Then you can go back to the terminal bash environment again.
$ python3 Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:10:52) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> exit()
- To get the current python version, add uppercase -V as the python command parameter like below.
$ python -V Python 2.7.10 $ python3 -V Python 3.9.1
- But how to make
python
command invokes python 3.7 or python 3.9 instead of python 2.7, you can follow the below steps. - Run
echo $PATH
command in a terminal, you can see python 3.9 and python 2.7 ‘s bin directory are all at the beginning of the PATH system environment variable.$ echo $PATH /Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:......
- Go to /Library/Frameworks/Python.framework/Versions/3.9/bin directory, list all the files inside the directory, you can see the command file
python3
is linked to another executable filepython3.9
.$ cd /Library/Frameworks/Python.framework/Versions/3.9/bin $ ls -l total 120 ...... lrwxr-xr-x 1 root admin 9 Jan 23 14:15 python3 -> python3.9 ...... -rwxrwxr-x 1 root admin 29008 Dec 8 01:17 python3.9 ......
- So you can run the command
python3.9
to invoke python 3.9 interactive interpreter.$ python3.9 Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:10:52) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
- If you want to make the
python
command invoke Python 3.9 by default, you can create a new filepython
and link it to thepython3.9
executable file in /Library/Frameworks/Python.framework/Versions/3.9/bin directory.# Below command create the new python and link it to file python3.9. $ ln -s python3.9 python $ ls -l ...... lrwxr-xr-x 1 songzhao admin 9 Jan 24 10:45 python -> python3.9 ......
- Now when you run
python
in the command line, it will invoke Python 3.9 as default.$ python Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:10:52) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
- If you want to make Python 3.7 the default Python interactive interpreter, you can add directory /Library/Frameworks/Python.framework/Versions/3.7/bin at the beginning of PATH system environment variable follow below steps.
- Run
cd ~
to go to your home directory. Runvim .bash_profile
to edit it.$ cd ~ $ pwd /Users/songzhao $ vim .bash_profile
- Add below code at the end of the .bash_profile file.
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}" export PATH
- Press
:wq!
in vim to save the changes and exit. - Run
source .bash_profile
command to make it effective. - Run
echo $PATH
to see the /Library/Frameworks/Python.framework/Versions/3.7/bin path is at the beginning of the PATH value, so the file inside this directory will be invoked first.$ echo $PATH /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin......
- Now when you run the command
python
, it will still invoke Python 2.7. - Create a file python and link it to file python3.7 in /Library/Frameworks/Python.framework/Versions/3.7/bin directory.
$ ln -s python3.7 python
- Now run
python
in command, it will invoke Python 3.7 by default.
3. How To Fix Still Use Old PIP Version Issue After Install Python 3.
- Now when you run command pip in a terminal, it still uses the pip command provided by python 2.
$ pip -V pip 6.1.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)
- Now we want to make pip command to execute the new pip version which is provided by python 3.
- Run command
which python
to show the current python binary file saved directory.$ which python /Library/Frameworks/Python.framework/Versions/3.9/bin/python
- Go to the above folder, you can see there is a pip3 file in the folder.
$ cd /Library/Frameworks/Python.framework/Versions/3.9/bin $ ls -l total 136 ...... -rwxrwxr-x 1 root admin 268 Jan 23 14:15 pip3 ......
- Run command
ln pip3 pip
to create pip file and link the file to pip3.$ ln pip3 pip $ ls -l ...... -rwxrwxr-x 2 root admin 268 Jan 23 14:15 pip -rwxrwxr-x 2 root admin 268 Jan 23 14:15 pip3 ......
- Open a new terminal, run command
pip -V
, now you can see it will use the new pip version.$ pip -V pip 20.2.3 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)