Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't update Python from 3.6 to 3.7 in MacOS

I have tried several commands to update python 3.6 to python 3.7 in homebrew on MacOS.

I have tried (as administrator user):

brew update

This claimed to install python 3.7

brew upgrade

brew link python3    

brew link --overwrite python3

brew unlink python && brew link python

brew switch python 3.7.0

brew switch python 3.7.5
Cleaning /usr/local/Cellar/python/3.7.5
24 links created for /usr/local/Cellar/python/3.7.5

After all those attempts, I still get this: python3 --version Python 3.6.5

Can someone please help me to get switched over to python3?

like image 975
raw-bin hood Avatar asked Nov 08 '19 23:11

raw-bin hood


2 Answers

Based on the comment:

which python3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3

Your python3 is not the same one installed/managed by Homebrew.
(Maybe it's from a Python .pkg installer for Mac?).

First, install it via Homebrew:

$ brew uninstall python3   # let's start from scratch
$ brew install python3

Check where it's installed:

$ brew info python3
python: stable 3.7.5 (bottled), HEAD
...
==> 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

If you need Homebrew's Python 2.7 run
  brew install python@2

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages
...

Notice that Homebrew installed it at /usr/local/bin/python3 and the site-packages are stored at the corresponding /usr/local/lib/python3.7/site-packages.

Next, you need to make sure your OS looks for python3 at that same path.

$ echo $PATH
/usr/local/sbin:/usr/local/opt/openssl/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

If you don't see /usr/local/bin there, add it to your PATH by adding this to your ~/.bash_profile:

export PATH=/usr/local/bin:$PATH

Then source the updated ~/.bash_profile (or restart your Terminal).

$ source ~/.bash_profile
$ echo $PATH

From the comment, if your PATH shows /Library/Frameworks/Python.framework/Versions/3.6/bin/python3, you'll either have to remove it by explicitly setting the full PATH in your .bash_profile or make sure it comes after Homebrew's Python in /usr/local/bin.

Finally, check that python3 is now correct:

$ which python3
/usr/local/bin/python
$ ls -l /usr/local/bin/python
lrwxr-xr-x  1 gino  admin  38 Oct  4 17:35 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3
$ python3 -V
Python3.7.5

Notice that python3 should be the python3 installed by Homebrew in the ../Cellar directory.

Can I easily change to the homebrew installation, or will I lose all my installed packages?

I would recommend re-installing the packages over at Homebrew's python3's site-packages folder. If you maintained a requirements.txt file for your Python projects, it's as simple as:

$ python3 -m pip install -r requirements.txt
like image 135
Gino Mempin Avatar answered Oct 10 '22 20:10

Gino Mempin


The final solution was that Python 3.7 was already installed and could be accessed using the command python3.7.

like image 20
raw-bin hood Avatar answered Oct 10 '22 19:10

raw-bin hood