The right way to Set up and Handle Python Variations in Linux

The Python programming language was launched in 1991. In all these years, it has gone by way of many modifications, with every model including and eradicating varied options. As a result of these modifications, software program written in newer variations of Python might or might not work with older variations.

This model mismatch prices developer expertise and productiveness, so it’s essential to discover ways to handle Python variations put in in your laptop to run all of them effectively. This tutorial exhibits you the way to do this.

The right way to Set up a Totally different Python Model

The best approach for Python model administration is utilizing the native package deal supervisor. Python comes put in out of the field on most Linux desktops. It has two main variations: Python2 and Python3. You possibly can affirm if these two variations can be found in your laptop by utilizing the next instructions:

# Test python3 set up
python3 --version
# Test python2 set up
python2 --version

To put in Python variations apart from the preinstalled ones, use the deadsnake PPA (Private Package deal Archive) in Ubuntu-based distributions.

If you happen to don’t have PPA enabled in your machine, allow it with this command:

sudo apt-get set up software-properties-common

Use this command so as to add the deadsnake PPA to your apt supply:

sudo add-apt-repository ppa:deadsnakes/ppa

Now you’ll be able to set up any Python model you need with the next command. Make sure you change “3.10” with the related model quantity.

sudo apt replace
sudo apt set up python3.10

Use the --version flag to examine in case your new Python model has been put in correctly.

Keep in mind, if you happen to examine your system’s Python model at this level, it nonetheless exhibits the variety of the preinstalled model.

Python Version

If you wish to use your newly put in model of Python because the default, you need to use the update-alternatives command, which helps set the precedence for various variations of the identical software program. Run the next instructions to set python3.10 because the Python model with the best precedence.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1

The second command marks python3.8 to your preinstalled Python model.

You need to use the next command to change between completely different Python variations.

sudo update-alternatives --config python

Handle Python Initiatives With Digital Environments

Python shouldn’t be good at managing dependencies. If you happen to use the default package deal installer, pip, or pip3 to put in Python libraries and packages, it can set up the packages globally. As Linux comes with a preinstalled model of Python and makes use of completely different packages to run the working system, manually put in packages within the world scope can disrupt its functioning. That is the place a digital atmosphere is useful. It’s an remoted Python atmosphere that has its personal instruments and libraries impartial of the principle setup. Consider a digital atmosphere as an remoted room that has minimal dependencies.

If you happen to don’t use digital environments, you don’t have any management on the variations of the packages you’ve utilized in your mission, which is problematic whenever you attempt to run your software program on a unique machine. Subsequently, it’s advisable to make use of a digital atmosphere in your Python initiatives.

Making a Digital Atmosphere With Venv

venv is the really useful method to create a digital atmosphere in Python and it comes preinstalled. You probably have by no means used venv, it is best to first set up its dependencies in your laptop with the next command. Change python3.10 to your put in Python model within the command.

sudo apt replace
sudo apt set up python3.10-venv

Now create a brand new digital atmosphere utilizing the venv package deal. We named our digital atmosphere “venv.” You possibly can title it no matter you need.

python3 -m venv venv
# Let's create a digital atmosphere named mte
python3 -m venv mte
Python Venv

After creating the digital atmosphere, activate it by sourcing venv atmosphere variables and instructions.

Now you’ll be able to see a (vnev) prefix in your terminal immediate, which signifies that your digital atmosphere is now lively and prepared for putting in dependencies. Let’s set up a brand new dependency known as “requests” inside our newly created digital atmosphere.

python -m pip set up requests

To deactivate the digital atmosphere, run deactivate inside it.

Python Venv 2

Making a Digital Atmosphere With Virtualenv

virtualenv is the preferred software to create Python digital environments. It’s a superset of venv, which signifies that virtualenv can do all of the issues venv can and extra.

You possibly can create completely different digital environments with completely different variations of Python utilizing virtualenv. It additionally lets you use completely different and particular variations of the identical package deal in initiatives – a function not obtainable within the venv package deal.

virtualenv has a command just like venv for making a digital atmosphere.

A brand new digital atmosphere named “venv” is created from the command above. To activate the digital atmosphere, supply the activate file.

Python Virtualenv

Now you’ll be able to see a (venv) prefix in your terminal immediate to point that the digital atmosphere has been activated.

To create a digital atmosphere with completely different Python variations, it is best to use the --python or -p flag and provides the placement of the Python executable. For instance, if you wish to create a digital atmosphere with Python 2.6, a really outdated Python model, the command ought to seem like this:

virtualenv --python="/usr/bin/python2.6" venv

Making a Digital Atmosphere With Miniconda/Anaconda

Conda is a package deal supervisor like pip. However not like pip, Conda helps many different programming languages and takes a unique strategy to creating digital environments. Conda is developed independently from pip.

You need to use Conda by putting in the Miniconda package deal. If you’re into knowledge science and machine studying, you may as well set up the Anaconda package deal which incorporates all of the data-science-related packages.

To put in Miniconda in your Linux machine, obtain Miniconda for the related Python model and run this shell script in your terminal to arrange Miniconda routinely.

./Miniconda3<model title goes right here>.sh

After set up, a default Miniconda atmosphere known as “base” is created. If you happen to run the conda set up command, the most recent variations of the packages you request are put in inside the atmosphere. In case your Conda atmosphere shouldn’t be activated, activate it utilizing this command.


Conda makes it straightforward to create environments for various Python variations. All you need to do is specify the right Python model within the command. Conda will then routinely obtain, set up, and arrange all of the dependencies for you.

For instance, if you would like Python model 3.7 in a Conda atmosphere, the command ought to seem like this.

conda create -n "myenv" python=3.7

After creating and activating this atmosphere, you need to use it to put in your favourite software program, similar to NumPy:

Run a Python3 Script With “Python”

It’s extra intuitive to sort python as an alternative of python3 to run a Python script. You can also make this swap routinely if you happen to use the “python-is-python3” package deal in Linux. After putting in this package deal, the python command routinely makes use of python3 binaries.

The “python-is-python3” package deal is offered in Ubuntu repositories and you may set up it utilizing the apt package deal supervisor.

sudo apt replace
sudo apt set up python-is-python3
Python Is Python3

Incessantly Requested Questions

Can putting in completely different Python variations break my system?

It’s potential. In case your working system wants some particular options of Python to work correctly they usually’re deprecated within the Python model put in in your machine, you could expertise instability in your machine. Within the worst case, your working system might break and you might have to put in it afresh.

The place do digital environments retailer Python packages?

Digital environments retailer packages in particular hidden directories inside the house folder. Totally different digital environments have completely different storage areas to make sure that they don’t pollute the system-level packages and the scopes and don’t intrude with the working of the working system.

The right way to take away a digital atmosphere?

You possibly can delete your digital environments very simply. Go to your mission listing and discover the listing named after your digital atmosphere. It shops all of the configurations of your digital environments. Delete this listing and you might be good to go.

Picture credit score: Hitesh Choudhary through Unsplash. All screenshots by Hrishikesh Pathak.

Is this text helpful?

Subscribe to our e-newsletter!

Our newest tutorials delivered straight to your inbox

Supply hyperlink

Leave a Reply

Your email address will not be published.