python runs older version after installing updated version on Mac

Question:

I am currently running python 3.6 on my Mac, and installed the latest version of Python (3.11) by downloading and installing through the official python releases. Running python3.11 opens the interpreter in 3.11, and python3.11 --version returns Python 3.11.0, but python -V in terminal returns Python 3.6.1 :: Continuum Analytics, Inc..

I tried to install again via homebrew using brew install [email protected] but got the same results.

More frustrating, when I try to open a virtual environment using python3 -m venv env I get

Error: Command '['/Users/User/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

I altered .bash_profile with

# Setting PATH for Python 3.11
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}"
export PATH
. "$HOME/.cargo/env"

And created a .zprofile based on this post with

export PYTHONPATH=$HOME/Users/User

and a .zshrc based on this post, but --version still throws python3.6.

I’m running Big Sur OS. Pip and homebrew are up to date and upgraded. Acknowledging that I’m totally foolish, what do I need to do to get python >3.7 running in terminal?

Asked By: WhooNo

||

Answers:

What you want to do is overwrite a python symlink.

After installing python via homebrew, you can see that python3.11 is just symlink.

cd /usr/local/bin; ls -l | grep python3.11

The result is:

lrwxr-xr-x  1 user  admin        43 Nov  7 15:43 python3.11@ -> ../Cellar/[email protected]/3.11.0/bin/python3.11

So let’s just overwrite it.

ln -s -f $(which python3.11) $(which python)
ln -s -f $(which python3.11) $(which python3)
ln -s -f $(which pip3.11) $(which pip)
ln -s -f $(which pip3.11) $(which pip3)

After these commands, pip, pip3, python3, python will invoke the version 3.11.

This command makes soft symlink.

ln -s

This command with -f option overwrite an existing soft symlink.

A soft symlink is similar to a shortcut.

In man page,which command is described as

which – shows the full path of (shell) commands.

Answered By: Constantin Hong