Ah…the virtual environment. If you’re here, chances are you’re just about finding out about the existence of these mysterious virtual environments and/or are trying to figure out why you need one. The first time I found out that I needed one, I was dutifully (and blindly) following the steps detailed in a YouTube video to set up a Python Flask App.
In this article, I’ll be outlining what exactly a virtual environment is, and why we need one in the first place – but not necessarily in that order 🙂
I’ll provide snippets of code (in the Python language) when examples are required while illustrating my points.
- WHYWhy do we need virtual environments?
- WHATWhat is a virtual environment?
Why do we need a virtual environment?
and when should we use one?
Before we go into what exactly a virtual environment is, I think an understanding of why they’re necessary would be beneficial.
For some context, imagine yourself as a veteran Python programmer with hundreds of packages (such as pandas, numpy, wtforms, jinja2…) all installed on your computer’s main Python installation (which is the installation that comes up with you type ‘python’ in the command prompt). Packages are installed to your main Python installation whenever you type:
pip install <package_name>
In this case, your computer (and your main Python installation) is acting as the environment. In other words, you’ve got all these packages installed in your main environment. Which is great, because you can easily call these packages whenever you need to write new code!
With me so far? Great! Now let’s introduce the problem: your fellow programmer-nerd-friend passes you his Python web application (his self-declared life’s work), and asks you to run it to ensure that nothing’s out of the ordinary. “No problem”, you reply confidently, as you load up his application on your unnecessarily (but still beloved) high-end computer.
If the program that your friend wrote was developed years (or sometimes even mere months) before, you’ll probably not be able to run his program the way he intended, especially if you’re trying to run it in your main Python environment. Issues ranging from minor bugs to full-blown crashes may pop up, seemingly out of nowhere.
The reason for this: when developing his program, your friend was using specific versions of certain packages, which contain their own objects and methods. The versions of the packages that he used for his program may very well be different from the versions of the packages installed on your computer! After weeks of debugging and trying to figure out what the problem was, you realized that your computer’s environment was using cx_freeze version 6.5.1 while your friend’s program was calling a deprecated function last seen in version 6.1. Depressing, huh.
It is at this point that you realized that simply uninstalling cx_freeze 6.5.1 from your main Python environment and installing cx_freeze 6.1 made all the bugs disappear! After telling your friend this, he brings to your attention a .txt file containing his program’s packages and their respective versions (aptly named requirements.txt).
One way to avoid this in the future, therefore, would be this: Uninstall all current versions of the required packages on your environment and reinstalling the relevant versions (as per the requirements.txt file) to run his program. Unfortunately, this might break the plethora of other Python programs that you’re still in the midst of developing – it’s also an immense hassle to repeatedly uninstall and reinstall different versions of packages!
Another, more elegant way to resolve this issue would be to create a new environment (instead of using your main Python one) and simply install all relevant packages there instead! This way, there is no need to uninstall any of your existing packages. Welcome to the Virtual Environment!
What is a virtual environment?
and how do I make one?
A virtual environment isn’t actually as complicated as you’d think – you can imagine it as a brand-new Python installation with no packages installed. Not very useful…yet. All you need to do to successfully run someone’s program is the requirements.txt file!
On Python, you’ll need to install the package virtualenv (on your main Python environment) in order to create and set up a virtual environment:
pip install virtualenv
Now to set up the aforementioned virtual environment (we’ll call this particular one test_venv). Ideally, you’ll first navigate to your project directory (the virtual environment you create will be stored as a folder in your current directory):
py -m venv test_venv
Next, we need to activate the environment that we’ve just created:
.\test_venv\Scripts\activate
You’ll see a (test_venv) appearing on your console preceding the input cursor, indicating that you’ve successfully activated the virtual environment:
(test_venv) C:\Users\...>_
All that’s left to do now is to install all the packages from the requirements.txt file:
pip install -r requirements.txt
Congratulations! You’ve installed all relevant packages and their corresponding versions into a virtual environment! Just remember: make sure that you generate your own requirements.txt file when you develop projects – you wouldn’t want anyone else to suffer like you were made to suffer…right? It’s always good practice to develop programs in a new virtual environment so that you’ll be able to install just the relevant packages. Most IDE’s (like Visual Studio Code and Pycharm) have inbuilt virtual environment-generating capabilities which essentially does everything outlined above (in far fewer keystrokes), but in case you went the manual route (as detailed above), you’ll also need to generate your own requirements.txt file containing all the packages you used:
pip freeze > requirements.txt
That’s all from me for today! Happy coding!