I've just installed Python version 3.11 (I also moved versions 3.8 and 3.9 to the trash from my Applications folder).
I can see it in the following:
$ myname@name-MBP miniconda3 % ls /usr/local/bin/py*
/usr/local/bin/pydoc3 /usr/local/bin/python3-intel64
/usr/local/bin/pydoc3.11 /usr/local/bin/python3.11
/usr/local/bin/python3 /usr/local/bin/python3.11-config
/usr/local/bin/python3-config /usr/local/bin/python3.11-intel64
(Any additional recommendations on whether I need to clean things up would be much appreciated.)
Checking python3 --version
still displays Python 3.8.13
.
First attempt to solve
Initially I tried installing it from the command line using homebrew
and specifying the version:
brew install [email protected]
I also tried using conda
, but neither of these worked.
Second attempt to solve
My initial thought was to check my PATH (I think this is how Python decides which version to use, but please correct me if I'm wrong).
This still only contained version 8 paths:
/Users/myname/miniconda3/lib/python38.zip
/Users/myname/miniconda3/lib/python3.8
/Users/myname/miniconda3/lib/python3.8/lib-dynload
/Users/myname/.local/lib/python3.8/site-packages
/Users/myname/miniconda3/lib/python3.8/site-packages
So I added the Python3.11 path to it using
PYTHONPATH="/usr/local/bin/pydoc3.11/:$PYTHONPATH"
export PYTHONPATH
Now it includes the v3.11 path when I print out sys.path
:
/usr/local/bin/python3.11
/usr/local/bin/pydoc3.11
/Users/myname/miniconda3
/Users/myname/miniconda3/lib/python38.zip
/Users/myname/miniconda3/lib/python3.8
/Users/myname/miniconda3/lib/python3.8/lib-dynload
/Users/myname/.local/lib/python3.8/site-packages
/Users/myname/miniconda3/lib/python3.8/site-packages
But the python3 --version
output is still unchanged.
Questions
I'm nervous to keep playing around with the contents of my path and entering random command line executions to try to solve this because I really have no clue what I'm doing.
What's happening here?
How can I get the output of python3 --version
to be 3.11?
CodePudding user response:
So first thing to understand is setting the variable PYTHONPATH
will not affect which version of python is executed by the shell. The shell (bash/zsh) only knows to scan the paths in the PATH
env var to figure out all the executables.
Now there are two ways to solve this.
1. Using the [email protected]
from homebrew.
There are several downsides to using this method. Currently, the default python3 by brew is 3.10.x
. Whenever you will install any cask or formula that depends on python@3, it'll invariably install the python@3 formula aka 3.10.x. Installing python3
will make brew symlink 3.10.x into /opt/homebrew/bin
.
Python 3.11.x can be used by installing [email protected]
and invoking python3.11
. This should drop you into the Python 3.11 interpreter. Append all python executable names with the version like pip
will be pip3.11
Trying to force 3.11 over 3.10 links will be complicated and cause instability. It'll only cause frustration during development.
2. Using VirtualEnvs
Your best bet for the most stable and no-headache approach to python is to create virtual envs using either venv
or pyenv
. use pyenv-virtualenv
for max ease of use.
One limitation of venv
is that it'll create a virtualenv of the same version that invoked it. Aka if you do brew install python@3 && python3 -m venv <folder>
, it'll create virtualenv of python3. For 3.11 you'll have to brew install [email protected] && python3.11 -m venv <folder>
. Pyenv on the other hand can install any version of python.
Go through https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/ and https://github.com/pyenv/pyenv-virtualenv to understand and learn more.