2

I want to ask you a question about the Python update. If I wanted to update my Python version from the 3.10 to 3.11, what should I do? And, most important, do I have to reinstall all the packages like Pandas, Numpy, Matplotlib, ...? In your opinion, when is needed to update it?

2 Answers2

1

Unless there is some security threat or some particularly new fun-ctionality, I wouldn't update too much between minor versions.

Then thanks to pip managing python packages is easy :

You can use pip freeze contextualized to a version, like :
python3.10 -m pip freeze

This will output a text with all libraries and their versions used by this specific binary.

You can put that into a file :
python3.10 -m pip freeze > /tmp/python3.10.pip.txt

And then install it with another version :
python3.11 -m pip install -r /tmp/python3.10.pip.txt

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • 1
    Note: Some specific versions of pandas won't work properly with python 3.11 – shaik moeed Mar 13 '23 at 18:35
  • 1
    Also might want to give a look to virtualenv, and docker maybe. – Loïc Mar 13 '23 at 18:40
  • So, don't I need to download again python from the website? If my version is 3.10.2, do I have to write it or 3.10 is enough? – Flavio Brienza Mar 13 '23 at 18:55
  • @FlavioBrienza it really depends on your OS and your aliases, when in command line, type in : python3 and then press twice, you'll get the python binaries in PATH. But then yes, python3.10 should cover all that minor version and you don't have to go below in sub-versions. – Loïc Mar 13 '23 at 18:59
  • So, can I do the entire process from the command line, just copying and pasting what you have written? – Flavio Brienza Mar 13 '23 at 19:18
  • Depends on the OS and terminal, what are you running? – Loïc Mar 13 '23 at 19:22
  • The pip freeze from a 3.10 env may be incorrect for a 3.11 env (example: [python-requires](https://packaging.python.org/en/latest/specifications/declaring-project-metadata/#requires-python) metadata and packages with [python_version](https://peps.python.org/pep-0508/#environment-markers) environment markers). I would not recommend to recreate like this, instead install the "top-level" packages you want such as numpy, matplotlib, and then let pip on 3.11 solve the dependencies again. – wim Mar 13 '23 at 19:45
  • Since 3.11 is relatively new, it's also quite possible/likely that some of the versions pinned in a 3.10 pip freeze _do not support Python 3.11 at all_, and will need to be updated to newer versions. – wim Mar 13 '23 at 19:48
  • I'm running Windows 11. But my doubts are: do I have to go to the python website and installing a new version? Should I add the new version to the PATH? Should I delete the older version? Should I download all the packages like pandas, numpy, ... again? On internet there is nothing that clearly explains it – Flavio Brienza Mar 13 '23 at 19:57
  • if you are on windows probably are you using the Anaconda distribution, whose engine behind the scenes is `conda`. In this case, try to follow the link in my answer below. – mattiatantardini Mar 13 '23 at 20:05
0

If you are on a Unix system (certainly Ubuntu, but I think also most of all the other Unix-bases OSs), you can have different python version installed.

Ubuntu

To install a python version, you can follow one of the many guides available in the internet, like this. If you want additional information about the security of the procedure, you can read this.

Then, you need to recreate an environment (virtualenv is strongly recommended!) . You can run:

virtualenv -p /usr/bin/<python executable of the wanted version> <venv-name>

or:

<python of the wanted version> -m venv <venv-name>

for instance: python3.11 -m venv myvenv.

Then, you can activate the environment and install the dependencies. In general you can restore the packages through the requirements.txt file or equivalent pyproject.toml (depending on the package manager that you use). But when changing python version, and especially when upgrading it, it is recommended to reinstall manually the "top-level" packages, as @wim stated, because a different package version (if existing) is required to work with the new python version.

Other OSs

If you are on Windows or MacOS, probably you are using an Anaconda or conda-based python distribution. In either cases, you probably want to follow one of the methods explained here.

After having upgraded python, you need to recreate the environment in an analogous way as it was explained for the Ubuntu case.

mattiatantardini
  • 525
  • 1
  • 5
  • 23
  • Thanks, but I do not use Conda, I use plain Python. I am on Windows 11. Do I need to uninstall the older version and installing all the packages once again? Is it enough to use pip freeze to keep the packages on the new version? – Flavio Brienza Mar 13 '23 at 20:27
  • I don't have much experience with plain python on Windows.. I guess that it is preferable to unistall the older version and install the newer one to avoid conflcts. Pip freeze ha the downsides already explained by @wim. Can't you take in consideration the usage of Anaconda, and in case yes, why? That should allow you to have multiple different python versions installed. – mattiatantardini Mar 13 '23 at 20:31