Managing multiple Python versions and virtual environments
Most of us work with a single Python version. We update the same when there is an update. But we can install multiple Python versions. It can be confusing managing multiple Python versions. This guide will take you through it.
Installing multiple Python version:
You get a installer for both windows and mac. For Linux you need to download the required compressed Python version and install it. Select your desired Python version and download from here.
sudo wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
sudo tar xzf Python-3.8.0.tgz
sudo ./configure --enable-optimizations
sudo make altinstall
Follow the above steps to install a particular version for Linux. Installing using a installer is straight forward.
Installation is done, but the real problem comes now…
Problems you face with multiple Python version:
- Creating virtual environments with a particular Python version
- Installing Python packages to a particular version of Python
I will be showing the steps in windows but it works the same in Linux and Mac.
⚠️ Note for windows users: Make sure Python is added to the path. Without this you wont be able to access the Python executable file.
Installing packages to particular Python version
Consider you have Python version 3.7 and 3.8 installed in your system. You want to install Virtualenv to version 3.8 and not 3.7.
python3.8 -m pip install virtualenv
By using the Python version and -m flag, you can access the pip of Python 3.8 and not 3.7.
Above image shows the contents of the Python folder after installation on Windows. Main file is python.exe.
If python3.8 -m isn’t working for you then change the name of python.exe to distinct names. So that they don’t clash with other Python versions. Change in the environment variables also. Now you should be able to access the executable file by the new name given.
Creating virtual environments with particular Python version
python3.8 -m virtualenv venv
Most of us will be working on Vs code. Vs code automatically picks up Python virtual environments if there are any. If not you can do “ctrl+shift+p” on Windows or “cmd+shift+p” on Mac and select the Python interpreter from there.
Hope this content helped you in some way.
Stay Curious… Stay Creative…