Why is Python version different in IDLE and Terminal

I have downloaded Python 3.12.0 from Python.org onto my iMac M1. I originally had folder versions 3.9 and 3.10, which I have now deleted. When running IDLE the heading is IDLE shell 3.12.0 and simple programs written here run OK. If I go into Terminal then I can run a *.py script OK but the version (Python --version) is 3.9.7. Dont understand why I’n not seeing Python 3.12.0 everywhere?

This means that there is a python 3.9.7 somewhere on your machine and it’s found (on the PATH) before 3.12 is found.

Multiple versions of Python can co-exist without problems. There are also multiple places where some or multiple versions might be installed:

  • inside Xcode
  • inside PyCharm
  • you might have one or more separate brew installs of python
  • you might have one or more conda installs
  • you might have multiple system-wide installs
  • (not an exhaustive list, since you can basically install python anywhere you like)

Which python is selected in terminal, depends on what is available, what the path is in your terminal shell, and how exactly you opened or called python.

First thing to check in terminal is:

ls -al $(which python)  
# and/or
ls -al $(which python3)

That might point to

/Library/Frameworks/Python.framework/Versions/3.9/bin/python3

You never need to remove python versions from that directory - and it might not always be safe to remove versions that you didn’t install yourself (versions that came with the OS) since (or if) you don’t know if some other app might be depending on those. But it’s fine to add new versions there, and reset the Current link (and the link /usr/local/bin/python3 (if that’s where your problem came from).

Hi Hans - thanks for reply. I found that I have … /opt/anaconda3/bin/python3 → python3.9. You say its fine to add more, but what would you advise in this case?

In that case, after opening your shell, you should just run conda deactivate.
If that doesn’t fix the problem, then you might need to tweak the settings in your ~/.bash_profile or ~/.zshrc file (either make sure that the Python 3.12 binary is somewhere at the front of PATH, or comment out the conda setup section that you may find there).

Alternatively, you can also keep using conda (which may be easiest). In your terminal shell, assuming conda is active, you can run for instance:

conda create -n py312 python=3.12
conda activate py312

This will create and activate an environment for Python3.12.
You can see if conda is activate, by running:

conda env list

If it is active it will show a lists of current env, and the active env is marked with an asterisk.
Or by running:

conda info

The first line will show: “active environment: None” if none is active.

2 Likes

If I remember correctly, python3.12 in terminal should run 3.12

Great, all working correctly now, including numpy and matplotlib. Very many thanks for your expert advice.

Hello again, Python 3.12 has stopped working again in Terminal. I note after using your “conda create …” and “conda activate py312” when using conda info the python version shown is still 3.9.7.final.0. Any advice on what went wrong? When running conda env list I get
base. * /Users//opt/anaconda3
py312 /Users/
/opt/anaconda3/envs/py312

Note my user name is between /Users/ /opt/

Just a bit more information here. I reloaded Python 3.12 from Python.org. File was loaded into downloads and I activated it and agreed to all conditions and ran new licence agreement, etc as program requested. When in the terminal window i ran your advised conda commands, finishing with conda activate py312. This appeared again to show that in"Desktop - -zsh - 80x24 we have Python 3.12.0.
However when opening Terminal again and running Python --version we get Python 3.9.7. Can you advise what changes are required?

What does

echo $CONDA_DEFAULT_ENV

show?
When you run conda env list what do you see? (You can take out the user name from the path)

On my machine I get:

$ conda env list
# conda environments:
#
base                  *  /Users/hansgeuns-meyer/anaconda3
py12                     /Users/hansgeuns-meyer/anaconda3/envs/py12
torch11                  /Users/hansgeuns-meyer/anaconda3/envs/torch11

$ python --version
Python 3.11.6

This means my current env is the ‘base’ one for Python 3.11. To switch to python 3.12 I need to run:

conda activate py12

After that my Python will be Python 3.12 and I get

$ echo $CONDA_DEFAULT_ENV 
py12

$ python --version
Python 3.12.0

Hello Hans,

The commands you advised work just as you described and Python 3.12.0 is seen as the version of python. However when just reentering Terminal again (or loading a Python file into Python Launcher) and using Python --version it still says the version is 3.9.7. I assume it must be something in the PATH (or similar) that redirects to an earlier version. Not sure how to change PATH or otherwise. Hope you can advise.

Regards
Alan

So, that’s not really a problem, is it? To use Python 3.12 you need to active conda and then the py12 environment (as you did).

The Python Launcher is a separate app. I don’t know how you installed that as app. If you can see that in your /Applications it should have been installed under /Applications/Python{some_version}/Python Launcher. If you see a folder /Applications/Python3.9 you can safely delete that whole folder.

Yes. That’s because one of the things that conda activate py312 does for you is to change the PATH, so that python will find that Python instead of the 3.9.7 Python.

PATH doesn’t specify programs, or versions of programs. It specifies folders where the operating system will look, to try to find programs with a specific name. When you try to use python as the name of an executable, without specifying a path explicitly, the operating system will try each of the folders listed in the PATH, to see if it has something called python in it (directly, not in a subfolder). If it does, it will try to execute that.

Every major home operating system - Windows, Mac and Linux - uses this kind of PATH environment variable, and it works in basically the same way.

Most likely, in a new terminal you get the 3.9.7 Python because it came with your computer. I’m pretty sure Mac works like Linux here, and will put the Python 3.12 executable in the same folder as the Python 3.9.7 one (and both will also have libraries somewhere else). Originally, the name python3.9 in that folder would be the actual Python program itself, and python3 and python would symlink to it. After installing Python 3.12 from python.org, that would not have changed the PATH or the symlinks, but it would add a python3.12 executable to the same folder (which was already on the PATH). The idea is, in a new terminal window, python3.12 will start Python 3.12, while python and python3 and python3.9 will start Python 3.9. You should be able to use system utilities to change the python and python3 symlinks.

Conda creates and manages “virtual environments” by temporarily changing the PATH (the changes only apply to the current terminal window, until you deactivate). It will put the folder for one of its copies of Python earlier on the PATH, so that the python command finds that instead.

Learning how to modify the PATH is a separate topic, but it isn’t normally necessary for working with multiple Python versions. If you want to use Conda to set up Python environments, you should stick with using Conda’s commands to choose which Python to run.

1 Like