how to update python version?
Update python on linux 2.7 to 3.5 - Stack Overflow
How to update Python version in Terminal? - Stack Overflow
linux - How do I update a Python package? - Stack Overflow
Videos
Hey all,
I'm trying to code in Python but I'm slightly agitated by the fact that I'm running an outdated version of python, 3.10, instead of 3.12. 3.10 came preinstalled on my system. When I try using sudo apt install python3 it says my python package is already up to date. so instead, i tried installing python 3.12 with the deadsnakes ppa (i was warned that this can cause issues on popos but quite frankly, i do not care) which technically worked, i do have python 3.12 installed on my system now, but in my terminal, typing python or python3 both lead to the old version, python 3.10, being launched. to launch 3.12, i have to type python3.12 in the terminal.
so... how can i just... update python? I don't want 3.10 on my system anymore.
if anyone can help, i will be very grateful.
In almost all cases, this is a bad idea. Changing the system Python can and quite often will break some other packages depending on the previous environment or version. Upgrading from Python 3.10 to 3.13 means quite a few things have been changed and/or removed, making such problems likely.
So, how can you use a more modern version of Python?
One way ist the way you have already gone, installing it and accessing it via python3.13.
Another way is using the deadsnakes PPA, see e.g. this answer by Hannu (but refrain from overriding your system default).
And then there are multiple ways to get access to a specific Python version within a specific project, here is a small selection:
- Pyenv is the classic solution for this. It allows you to download and install basically any Python version (even alpha and beta) and activate them dynamically or use them as the base for a virtual environment.
- UV, a upcoming Python dependency manager, can manage Python versions as well. You just have to specify the desired Python version in a
.python_versionfile in your project root directory. - Conda, another dependency manager, but not limited to Python, can also manage multiple Python versions.
A simple "clean" way to install a more recent Python3 than present in Ubuntu is the deadsnakes ppa:
Howto set the default version:
https://www.debugpoint.com/install-python-3-12-ubuntu/
... at "Use Python 3.12 as the default Python3"
The basics for installation:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12
https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
The ppa gets updated, so a more recent one may well be present;
also ALWAYS verify the correctness of what is stated above.
//install python 3.6
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt update
sudo apt install python3.6
//change default python
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python
//view default
python -V
You still have Python 2 installed, and the python command is still set up to invoke that version by default.
Try running your scripts like this:
python3 yourscriptname.py
In /usr/bin/, "python" is actually a symlink to python2.7. If you'd rather not have to type the 3 at the end whenever you use python, change that symlink to point to python3 instead. Then python will run Python 3.5 and you can use python2.7 or just python2 to run Python 2.7 scripts.
EDIT: Alternatively, you could put alias python=python3 in your ~/.bashrc file to do the same thing, but without needing root access and for your user account only.
As others already noted, bare sudo apt-get install package will install latest available version, replacing the older one if needed.
But with some software (among which is Python) the situation is somewhat different. Some major, very- and incompatibly-different versions get their own packages. For instance, Python 2.6, Python 2.7, Python 3.1 all live in separate packages on Ubuntu.
Of particular importance is the fact that one of Ubuntu policies is to extensively use Python for writing end-user software. So in fact, fairly large part of the system is written in Python. At the moment, the code runs on Python 2.6 — so this version is the default upon installation; and the code won't easily run on, say, Python 2.7 — because incompatibilities exist. To switch the system to Python 2.7 there needs to be done a piece of work, consisting of updating and re-testing all the scripts. This can't be done easily; that is, you can't just "switch" your system to Python 2.7 and delete the older version.
But. If you don't care about fancy gears of your system and just need newer Python — see no obstacles. Go and sudo apt-get install python3 and code for 3.x Python bravely; just remember to launch your scripts with python3 and use #!/usr/bin/env python3 shebang line.
Upd: I keep seeing this upvoted; notice that this is a fairly old answer, things have changed.
What to learn next
From a superuser perspective (not Python developer's), the next things I'd suggest learning to use:
pip/pip3/python3 -m pip— this is thenpmfor Python. Quick tip: trypip3 install --user howdoi(may need toapt install python3-setuptools python3-piponce, before that works). Then for example,howdoi --all compile python3 ubuntu.The
virtualenvtool. It's 100% developer-oriented, but you'll likely need to use it (perhaps underneath a few wrappers, such astoxorpipx) to work with people's source packages.
Ruby'sbundleror Cabal sandbox may be familiar analogues.The
condatool — which is a totally separate python package repository and installer (think: fork of PyPi).
There's humongous variety of tools in the Python ecosystem in 2020. At the very least, make yourself comfortable with pip before going deeper.
Basic pitfalls
For the brave but unwary, a few classic pitfalls when trying to manually set up a newer CPython on Ubuntu.
Leave
/usralone; you can look but you don't touch. Leave it todpkg, save yourself some confusion. You have the whole/usr/localat your disposal:sudo chown -R `whoami` /usr/local pip3 install --prefix=/usr/local pydfCompiling CPython from source is well-explained on the web; just don't forget your
/usr/localprefix. This is the best way to manually test patches and/or pre-releases (those alpha-, rc- builds) of CPython itself. To wipe built artifacts, you can justrm -rf /usr/local/*; sudo ldconfig.Finding a PPA is decent option too; keep in mind that a PPA is just someone else's private build. Look for credible PPAs with CI/CD running.
sudo apt-get install python 3.3.3
this is for python(3.3.3) for different version the corresponding version number should be used.
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:
- asdf
- pyenv
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:
- Poetry
- Pipenv
I just had the same problem and I'm sharing this in case this helps anyone else. Note: I am on a Windows computer. Initially, I thought all I had to do was download the newest version of Python from the website. However, my terminal was printing:
python --version Python 3.12.1
python3 --version Python 3.10.3
py --version Python 3.13.1
So the first thing I did was type scoop install python because I thought it might be a problem with that command line installer. But even when I did that, it was now printing out:
python --version Python 3.12.1
python3 --version Python 3.13.1
py --version Python 3.13.1
So eventually I made my way to my environment variables and removed an install of Python 3.12 from my PATH for my local user profile. Now all versions of Python correctly print their intended version!
python --version Python 3.13.1
python3 --version Python 3.13.1
py --version Python 3.13.1
The best way I've found is to run this command from terminal
sudo pip install [package_name] --upgrade
sudo will ask to enter your root password to confirm the action.
Note: Some users may have pip3 installed instead. In that case, use
sudo pip3 install [package_name] --upgrade
You might want to look into a Python package manager like pip. If you don't want to use a Python package manager, you should be able to download M2Crypto and build/compile/install over the old installation.