Maintaining multiple versions of Python using Anaconda
There can be any number of reasons to require different versions of Python and we can do this easily using Anaconda.
Many popular operating systems come pre-installed with a version of Python. It is not recommended to remove or change Python’s version if it came by default with the OS installation. On Windows, it is mostly not the case and you can leisurely choose the version you want from the Microsoft Store. But most popular Linux distros and Mac do not provide this luxury. If you are using a rolling release distro like Arch Linux or Garuda Linux you get the latest Python version (currently 3.10.2) which can cause problems as many popular packages won’t be having support for your Python installation. For example, the current TensorFlow version 2.7.0 provides support only till Python 3.9. This is one of the reasons why beginner-friendly distros like Linux Mint or Ubuntu come with an older version of Python (currently 3.8.10). Many times we want to run some old code that uses some deprecated packages like the many TensorFlow 1.15 scripts we find on the web, whose support ended with Python 3.7. Sometimes we just want to tinker with the latest features Python has to offer. There can be any number of reasons to require different versions of Python in the same system and we can do this easily with the help of Anaconda (no this is also not a 🐍).
What is Anaconda?
Anaconda is one of the most popular tools used for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.). It provides distributions of Python and R (another popular programming language used mostly for statistical computing).
The basic requirements are:
- A working computer
- Anaconda (Install Anaconda)
- Terminal (it’s not a requirement per se as we can also use Anaconda Navigator but I prefer terminal)
- A working 🧠 (it’s not a requirement per se as the steps are simple and just copying-pasting will do the job but it is a good-to-have)
Mainly, we need conda which is a package manager for Anaconda, or Anaconda Navigator if you prefer GUI (why?).
Firstly, we will check for Python versions that are available to us. To do this we require a little bit of regex 😈, or just copy the below command.
conda search “^python$”
As for those who are interested in the regex part, ^ represents the starting position and $ represents the ending position. So basically, we are searching for packages in conda with the name “python”. You can enter the regex on the regex101 website for a better understanding.
Next, we will create the environment with the required Python version.
conda create -n <environment-name> python=<version> anaconda
You can enter any name you want but make sure the version you enter is available.
For example, if you want Python with version 3.8.10, run
conda create -n python3.8 python=3.8.10 anaconda
Yeaaaa!!!!! The environment is created. Now, we can use it.
To activate the version you want just run,
conda activate <environment-name>
Using Anaconda Navigator
It is very simple to create a new environment using Anaconda Navigator. The below two screenshots show the process completely.
First, open Anaconda Navigator and go to Environments in the left navigation bar. In the bottom click on Create.
A Create new environment popup will open. Write a name for this environment and choose the version you need and click on Create. Voila it’s done. 🎉🎉🎉
Bonus: Using Pipenv to maintain different package versions for each version of Python
I like using Pipenv to install packages. It makes it easier to use and maintain virtual environments and helps ensure that everyone working on the application is using the same version of every package. But when I created a virtual environment, Pipenv by default used the Python installed in the system even though I have activated the conda environment. The reason for this is that I appended anaconda at the end of the PATH variable which by default is prepended to the PATH. So, the workaround I found towards this is to specify the path of the Python I want to use with Pipenv.
To do this first activate the conda environment of the Python version you want. Then specify the Python to Pipenv using
pipenv --python=$(which python) --site-packages
Now, Pipenv will use the current activated Python for the virtual environment.