15

I updated my Ubuntu from 22.04 to 24.04 Previously python 3.10 was getting used Now the default version is python3.12

  1. Python 3.12 seems to call for virtual environment usage, Have been going through some documentation and forums, but was thinking if it is better to go back to python3.10
  2. Would you recommend update-alternative where I can have both versions?
[sudo] password for meena: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package python3.10 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'python3.10' has no installation candidate

Meenohara
  • 263
  • 1
  • 3
  • 9

6 Answers6

12

Python 3.12 seems to call for virtual environment usage, Have been going through some documentation and forums, but was thinking if it is better to go back to python3.10

No, it's not better to go back to Python 3.10.

Or more precisely, it's not better to change the system installation of Python back to 3.10. Your system is built in a way that it expects to have Python 3.12 available, and if you remove it, various things will stop working.

There are two changes you can and probably should make to your Python usage:

  1. Virtual environments are now a recommended practice for all versions of Python, so you should start using them. There are numerous tools that you can use to create and manage virtual environments; for example, I particularly recommend pipx for installing Python applications, i.e. anything you intend to run, but not use in your own code. If you're going to be writing your own Python code, on the other hand, consider something like hatch (my favorite), pdm, flit, or poetry (which is a little less polished than the others; it slightly predates some of the modern standards in Python development). You might also consider pipenv, which is a simpler tool that just creates a virtual environment and manages the list of packages installed in it, without the development project features the others have.

    You can also create virtual environments manually using the venv module which is built into the Python standard library (python -m venv <path>), or virtualenv which is a slightly more sophisticated version of the same thing. Each of these tools has its own unique workflow so you can try them and see what you like.

  2. If you need access to different versions of Python other than 3.12, I'd suggest installing them "manually", not using apt. Perhaps the easiest way to do this is with something like pyenv, which gives you a fairly simple tool that can install, upgrade, and uninstall many different versions of Python without touching the one used by your system. After you install the version you need, you can then use it to create virtual environments, or you can have your tool of choice use it to create virtual environments.

There are other tools like rye and uv (now in the process of merging... sort of?) which try to do all of the above things - they will let you create and manage virtual environments, and also let you install and manage versions of Python. Feel free to give those a try if that sounds useful.

David Z
  • 10,945
  • Thank you Have one doubt about these virtual environments. Somewhere I had read they can be configured sepaartely for different projects or have a common one, unable to find now What would you recommend? Have one common virtual env or have separate venvs? – Meenohara Oct 18 '24 at 12:39
  • Separate venvs, for sure. venvs are relatively lightweight, so you don't have to worry about creating a new one whenever you need it. – David Z Oct 18 '24 at 18:12
7

Don't change the default python version. That will destroy Ubuntu.

Instead, use a virtual environment with Miniforge.

Download the 64 installer for linux, as described here.

Now install it.

curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh

Now create a virtual env with python3.10

conda create -n myenv python=3.10

Activate this environment with

conda activate myenv

Now run python3, and it should be python3.10.

5

Here's how you use Python and its packages, in order of my preference:

1. Use system Python and system packages

On Ubuntu 24.04, that means Python 3.12. You install required packages through apt (e.g. apt install python3-numpy or apt install python3-django).

  • Lowest barrier to entry
  • Security updates for all packages are managed by your distribution vendor.
  • You only get access to Python packages that your distribution offers, at whatever version they provide.

2. Use system Python and Virtual Environment

Again, for you that means Python 3.12. Set up a Virtualenv per project and install packages there (e.g. virtualenv venv; source venv/bin/activate; pip install whatever).

  • Slightly more work, but not too much.
  • You get full control over what packages you have access to, and their versions. These can differ from one project to another.
  • Keeping the packages in the Venv up to date with security updates becomes your responsibility.

3. Use pyenv or something like anaconda

Unlike the previous solutions, this allows you to get different Python versions. Try to avoid this, if you can. If software works on Python 3.10 but breaks on Python 3.12, the better approach is to fix the software.

Never use pip as user outside a venv (or pyenv or anaconda, or...)

This installs software "globally" under your user account. It becomes hard to manage, and you don't get isolation between projects.

NEVER use pip as root outside a venv (or ...)

Same problems as above, but bigger. May break your system.

marcelm
  • 940
  • 7
  • 11
4

I've found that pyenv is more comfortable to use than other tools like anaconda.

You can find the installation instructions here: https://github.com/pyenv/pyenv#automatic-installer

After installing it, using it as simple as running

pyenv install 3.10

to install a given python version.

If you want to use python version globally, you can set it with

pyenv global 3.10

and if you want to set it only on a project, you can do so with:

pyenv local 3.10

This will create a .python-version file containing the version, and pyenv will automatically run that version when you run python in that directory. This is useful when you're working on various projects that require different python versions, which can happen often in a professional setting or with certain OSS projects.

Do keep in mind that this gives each python version a virtualenv, and that packages you installed on system python, even if they were on the same version at the time, will not be accessible.

ave
  • 311
1

Just because I didn't see anyone mentioning docker:

docker run -it --rm python:3.10

If you don't have docker installed:

sudo apt install docker.io

This executes python in the version you specify in the tag. You can pass in any version, that it will work. Way quicker to setup than having to install different versions and has a better isolation from the rest of your OS. If you need to run files inside your docker container, use the -v option to mount your filesystem to the containers' filesystem.

Of course, the utility of this solution depends highly on your use case. If you want to read more about docker, see its documentation: https://docs.docker.com/

-4

Follow below steps. This works for me.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
apt-get install python3.10
unlink /usr/bin/python3
ln -s /usr/bin/python3.10 /usr/bin/python3