How to leave a single version of Python on Linux/Ubuntu?

Asked

Viewed 1,188 times

0

Multiplas Versões do Python 3

I even understand the fact that there is Python2.7, which in itself is quite annoying because every time I lower the modules to the wrong place, but when I tried to upgrade the version 3.6 to 3.7 ended up installing another one. How to solve?

  • It is indicated that you stop installing only with pip install and start using python3.6 -m pip install, because this way you have explain the version used. Better than that is if you create the custom of creating virtual environments.

  • And do not try to uninstall version 2 of your system, this may break many internal dependencies of the operating system itself

  • There is no way, because there are numerous other software that use Python 2 and Python 3, unfortunately we are all doomed to keep the 2 on the machine for the foreseeable future.

  • Only if you use Ubuntu (and similar). The rolling release distributions already come only with Python 3.7

  • ... or pip3 install ...

1 answer

1

TL;DR:

You don’t have to worry about that - which versions of Python are installed for internal system use (in this case Ubuntu) are a problem of it. To work with Python you must learn to manage isolated environments (virtualenv), and install locally the Python version and libraries in the desired version.

Original response

Actually, it is not interesting to leave a single version of Python - you are asking one thing for the wrong problem.

What you need to know is as handle multiple versions of Python on the same machine.

The operating system naturally will install more than one version of Python - even if the main packages of the system are already using Python 3, and the system may have only one Python 3.7, just some software that needs Python 2 as a requirement to be installed (for example, GIMP 2.10)and Python 2.10 will be installed as well.

What you need to learn is to isolate all your Python projects using "virtualenvs" - try to start with this article.

Briefly, a virtualenv, or similar technology, such as "pipenv" or "buildout", allows you to create an isolated environment in a folder, where you will have a pre-selected version of Python, and will install all the modules you need for a given project in that folder. These Python modules are not seen by the operating system, and work independently. Libraries and third-party packages are installed in virtualenv with the command pip (and never with the system package manager, such as apt or dnf/yum)

It solves a lot, an almost infinite amount of problems. For example, it is common for Python web projects to require dozens of external dependencies - it would be enough if one of those dependencies had a different version of the package than is available as a package from your system, and you wouldn’t be able to develop the system on your computer. (Manually updating a package installed by the system manager is to ensure that you are corrupting your operating system and will need to reformat soon).

So, for example, let’s assume that I’m on an Ubuntu 18.04 and install some application, packaged for Ubuntu, that uses Django as a base. The application uses Django 1.2 - an old version. "apt" will take charge of installing this version of Django in Python system.

Then I decide to start a new system, in Django - I want to use the latest version - the 2.2 - I won’t try to fight and force Django 2.2 into the /usr/lib/python ...folders. just to start, this would destroy the application I installed with APT that uses Django 1.2.

I go in my user folder to create a "virtualenv", with the Python version I want, and install Django and the desired packages there - in general come newer versions than those available from the operating system.

The command sequence is (on Linux):

python3.7 -m venv env
source env/bin/activate
pip install django

In windows:

python3.7 -m venv env
env/Scripts/activate.bat
pip install django

The first line creates the isolated environment, in a folder with the name "env" - this folder contains a Python executable and a lib directory. The second line "activates" virtualenv - in every shell (cmd, in Windows), where you work with the project, have to execute the file commands activate. This puts the name of virtualenv between relatives at the beginning of the prompt - it will appear something like (env) /home/epx/$ - indicating that virtualenv is active.

And the third line is all that is required to install the latest version of Django, regardless of which Django is available for your system, and independent of other projects on the same computer. And even more: with this it is possible to reproduce the installation of your projects with exactly the same versions of Python packages on any computer - so it is possible to develop your project on Ubuntu 19.04, and put the same to run on a Centos server 2017 - there are a number of things to learn about virtualenv and setup of Python projects, but the basis is this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.