Home > Back-end >  How to update Python version in Terminal?
How to update Python version in Terminal?

Time:10-27

I've updated my version of Python to 3.11, but Terminal is printing different versions, depending on what command I enter.

Entering python3 --version prints Python 3.9.13.

Entering python --version prints Python 3.9.6.

When I go to the actual Python framework, I can see that 3.11 is installed and is the current version, per the shortcut there. There are multiple versions of Python--Python 3.7, 3.8, etc.--there; perhaps this is the issue.

I've looked into uninstalling some or all versions of Python, but I worry that will just make it worse--I'm not the most experienced programmer.

I've also tried adding an alias to the .zshrc file per other posts, but it didn't work. I did save the file correctly, for what it's worth. Any advice is appreciated.

CodePudding user response:

Updating Python in Mac

Updating Python on a machine with macOS is a lot easier than updating it on a Linux machine.

A Mac can have more than one version of Python installed. Therefore, you can update Python by visiting https://www.python.org/downloads/mac-osx/, downloading the installer, and running it.

If you have Homebrew installed on your Mac, you can run the following command on the Terminal:

        brew install python

After the process completes, your computer will have the latest version of Python 3. You can verify this by running the command:

        python3 --version

If you want to upgrade pip and add a new library to it, you can enter the following command:

        pip3 install <PROJECT NAME>

CodePudding user response:

I think you might be missing some foundational knowledge about how versions are selected from a terminal.

You'll want to do some learning about PATH environment variable and how that relates to python versions. https://www.tutorialspoint.com/python/python_environment.htm#:~:text=The path is stored in,sensitive; Windows is not). The path that you're using is probably going to one version of python in your terminal but you have settings in whatever program you are using for the "python framework" is pointing to a different version. Also look a little into .bashrc and .zshrc files

Python environments can be a bit annoying to manage at times. I'd recommend reading about pyenv and anaconda so that you can manage them.

Good luck :)

TLDR: Make sure your path for the terminal you are using is pointing to the correct location for the python version installation you want to use.

CodePudding user response:

A terminal in a unix-based OS (including Darwin / MacOS and Linux) will search for the command you have typed in several places, in order, and the first match found will stop the search from finding other possible commands with the same name.

To simplify the answer, I'll limit this to the bash shell as I don't use ZSH, but I think the answer should be similar in both shells.

After you type in a line of input and hit enter, your shell will modify its input so that it expands variable interpolations and such things and then will take the first token (first "word") and search for that word as a command name.

Commands can be found in several places, from builtin commands, to aliases, to shell functions and if the command can't be found there, the shell will begin looking at specific locations in your filesystem to look for files that are named the same as your command. The first match wins, so if you have several pythons in your PATH, the first one will be the one that is selected.

The set of locations in your filesystem the shell will look for the command is stored in an environment variable called PATH in the shell and is generally available to view and set.

to see what your path is:

echo ${PATH}

or. for slightly easier reading:

echo ${PATH} | sed 's/:/\n/g'

which python will let you see what python version your shell is finding first in the PATH and type python will let you know if for some reason you have a function or alias named python that happens to overshadow your filesystem python executables.

Also, bash maintains an internal hash table of where it has previously found commands. So if you type python and bash finds it in /usr/bin/python, then it will remember that for the rest of the session. If you'd like it to look again, because perhaps you have installed a new python you expect it to find first, you can type hash -r to reset the hash table to a blank new one.

All that being said, if you are starting to develop in Python, you're going to need to handle different versions of Python.

To start down that road, check out:

I recommend asdf as it can work for many things, not just python. Also, you'll probably want a decent dependency manager sooner rather than later for your project, so I'd check out poetry and pipenv:

  • Related