1 min read

How to Use Different Python Versions with Virtualenv

Learn how you can use Python Virtual Environments (venv) to easily use different python versions
How to Use Different Python Versions with Virtualenv
Photo by Fotis Fotopoulos / Unsplash

Python is sometimes a big mess to work with. Somedays you want to use the latest for all its bug fixes and features, but other days you need to use a specific version since your favorite python library doesn't support the latest.

So how can we quickly get started with let's say Python 3.9? This is what I aim to explain in this article!

Virtual Env

Python has a concept named "virtual envs". This concept allows you to easily use any python version with its own specific set of dependencies, creating an isolation between your different projects. It is also quite handy when you are developing a library, as the dependencies you install, typically translate 1:1 to the dependency requirement of the library itself.

Setting up your Python Version

# Configure the VirtualEnv Command
pip install virtualenv

# Install your Python Version
sudo apt update; sudo apt install python3.9

# Create a virtual env with your python version and location
# - Python Version: 3.9
# - Location: ~/.venv/myenv
virtualenv -p /usr/bin/python3.9 ~/.venv/myenv

# Activate the Environment
source ~/.venv/myenv/bin/activate

# Deactivate the Environment
source ~/.venv/myenv/bin/deactivate

Conclusion

Now you are completely up and running with the python version you wanted!