In 1991, the Python programming language was released. It has seen numerous revisions throughout the years, with various functions being added and removed in each edition. These adjustments may or may not make software created in more recent versions of Python compatible with earlier ones.
It’s crucial to learn how to handle the Python versions installed on your computer in order to run them all effectively because this version mismatch reduces developer experience and productivity. How to achieve that is demonstrated in this lesson.
How to Install a Different Python Version
Using the native package manager is the simplest method for managing Python versions. Most Linux PCs already have Python installed. Python2 and Python3 are the two most prevalent iterations. Using the following commands, you may determine if your machine has these two versions:
Make sure Python 3 is installed.
Python version 3.
Check the installation of Python 2
Python version 2.
In Ubuntu-based systems, utilize the dead snake PPA (Personal Package Archive) to install Python versions different than the ones that come preinstalled.

Use this command to activate PPA if it isn’t already enabled on your computer:
Install software-properties-common with sudo apt-get
To add the deadsnake PPA to your apt source list, run the following command:
Add-apt-repository ppa:deadsnakes/ppa with sudo
With the following command, you can now install whichever Python version you like. Make careful to substitute the appropriate version number for “3.10”.
apt update with sudo
install Python 3.10 with sudo apt
To verify that your new Python version has been installed correctly, use the —version flag.
Python version 3.10
Remember that at this time, the Python version displayed on your system still refers to the preinstalled version.
Use the update-alternatives command to set the priority for various versions of the same programme if you want to make your freshly installed version of Python the default. To make Python 3.10 the Python version with the highest priority, issue the following instructions.
Python 3.10 can be installed by running sudo update-alternatives —install /usr/bin/python.
Python 3.8 is available by running sudo update-alternatives —install /usr/bin/python.
The second command adds Python 3.8 to the version of Python that is already installed.
The command shown below can be used to change Python versions.
update-alternatives —config python in sudo
Manage Python Projects With Virtual Environments
Python struggles to effectively handle dependencies. Python libraries and packages will be installed globally if you use the default package installer, pip, or pip3. Python is preinstalled on Linux, and the operating system is run using a variety of packages, therefore manually installed packages in the global scope can cause problems.
A virtual environment is helpful in this situation. It is a separate Python environment that operates independently of the main setup and has its own tools and modules. Imagine a virtual world as a closed-off space with few dependencies.
If you don’t use virtual environments, running your software on a separate machine will be difficult since you have no control over the versions of the packages you used in your project. Consequently, using a virtual environment for your Python programmes is recommended.
Creating a Virtual Environment With Venv
The suggested method for building a virtual environment in Python is called venv, and it comes preinstalled. If you have never used venv, you should first run the following command to install its prerequisites on your machine. In the command, replace python3.10 with the Python version you installed.
apt update with sudo
install python3.10-venv with sudo
Now use the venv package to establish a new virtual environment. Our virtual setting was given the name “venv.” You can give it whatever name you like.

venv python3 -m venv
Make a virtual environment called mte, please.
Python 3 —m —venv —mte
Activate the virtual environment by sourcing venv environment variables and commands after it has been created.
Also Read: What Is Linux Operating System and How Does It Works
venv/bin/activate source
The (vnev) prefix you can now see in your terminal prompt indicates that your virtual environment is now active and prepared for dependency installation. Let’s add a new dependency to our brand-new virtual environment called “requests.”
requests made with python -m pip
Run deactivate inside the virtual environment to turn it off.
deactivate

Venv 2 in Python
Using Virtualenv to Create a Virtual Environment
The most widely used tool for creating virtual Python environments is virtualenv. Since virtualenv is a superset of venv, it is capable of everything venv is capable of and more.
Using virtualenv, you may make various virtual environments for various Python versions. Additionally, it offers a feature that the venv package does not: the ability to utilize several and particular versions of the same package in projects.
Similar to venv, virtualenv offers a command for building virtual environments.
venv virtualenv
The aforementioned command generates a brand-new virtual environment called “venv.” Source the activate file to turn on the virtual environment.
venv/bin/activate source
Virtualenv for Python
Your terminal prompt will now have a (venv) prefix to show that the virtual environment has been started.
Use the —python or -p flag and specify where the Python executable is located to create a virtual environment with different Python versions. For instance, the command should resemble this if you want to establish a virtual environment using Python 2.6, a very old Python version:
/usr/bin/python2.6 virtualenv —python venv
Virtualenv1

Miniconda and Anaconda for Virtual Environment Creation
Similar to pip, Conda is a package manager. Conda, however, supports a large number of additional programming languages and approaches the creation of virtual environments differently than pip. Conda is created separately from pip.
Installing the Miniconda package will enable you to use Conda. You can also install the Anaconda package, which contains all the packages relevant to data science if you are interested in data science and machine learning.

Download Miniconda for the appropriate Python version, then run this shell script in your terminal to automatically install Miniconda on your Linux machine.
Version name goes here:./Miniconda3
Following installation, the “base” Miniconda environment is generated by default. The most recent versions of the packages you request are installed in the environment when you use the conda install command. Use this command to activate your Conda environment if it isn’t already.
base conda activate
Also Read: How to View WebP Images on Linux: follow These Easy Steps to View
Miniconda
Conda makes it simple to build environments for several Python releases. All you need to do is make sure the command contains the correct Python version. The dependencies will then be downloaded, installed, and configured for you automatically by Conda.
For instance, the command should seem as follows if you want Python version 3.7 in a Conda context.
python=3.7 conda create -n “myenv”
You can use this environment to install your preferred applications, such as NumPy, after creating and activating it.
install numpy conda
Run a Python3 Script With “Python”
To launch a Python script, simply type Python rather than Python 3. If you use the “python-is-python3” package under Linux, this switch can be made automatically. The python command will automatically utilize python3 binaries after this package has been installed.
You can use the apt package manager to install the “python-is-python3” package from the Ubuntu repository.
apt update with sudo
Install Python-is-Python3 with sudo apt
Python is version 3.

FAQ
Can Upgrade My Python Version Damage My System?
It is doable. You can suffer instability on your computer if some specific Python capabilities that your operating system needs to function properly are deprecated in the Python version that is currently installed on it. In the worst-case scenario, your operating system can malfunction and you might need to reinstall it.
Where Are Python Packages Kept in Virtual Environments?
Packages are kept in distinct hidden directories under the home folder by virtual environments. To avoid contaminating system-level packages and scopes and interfering with the operation of the operating system, distinct virtual environments have independent storage locations.
How Can a Virtual Environment Be Removed?
Your virtual environments are very simple to remove. Locate the directory for your virtual environment in your project directory. It keeps track of all the settings for your virtual environments. You are now good to go if you delete this directory.
For More Information Visit Our Site: https://www.techllog.com/