Introduction
As developers we dislike when things go wrong. The only way we can minimise anything going wrong is if we, personally, take responsibility for every step along the way to our goal. However, this is an extreme position to take and there are no developers (that I’m aware of) that build their own computers and operating systems before they can begin to work on their latest project. A balance has to be struck, and some things are more complicated than others. Where you strike the balance is really about identifying which things are hard and which are easy. Which can you realistically do and which are impossible. In my experience, virtual environments are one of those things that are actually pretty easy, but perceived as very hard by new developers. New devs often turn to GUI-based environment managers like anaconda, or even our own Ansys Python Manager, to avoid learning to work with virtual environments on the command line*. But even if you are going to turn to these GUI-fronted package managers when working with PyAnsys or otherwise, it is useful to know about what’s going on under the hood when you use them. These tools will not always be available to you in the future, but so long as you have a computer and an installation of Python you’ll have a command line/terminal and the power to make virtual environments. This article is all about that.Installing Python
When you install Python for the first time as a budding developer everything is free and full of potential. The landscape is vast and offers a wealth of bounties over every hill and in every cleft in the land. What could be hiding round the next corner? It’s unknown and exciting. However, eventually the honeymoon period wears off and you’re confronted with the cold reality of programming. Many of those hills hid rivers of blood and those inviting clefts in the land? They were hiding peat bogs, and quicksand. Of course, in the meantime you’ve also fallen in love with navigating the unforgiving landscape of programming and you live for it. No one ever said development isn’t a mixed bag. So one day, you’re traipsing across some new low hills from a package you recently upgraded, exploring some new features deftly swinging over a pit here and fending off a cave troll there (you are naturally using this new landscape to workaround a customer requirement in your latest project). When all of a sudden, a previous customer (who used the same project) calls to tell you they lost their cat in one of the peat bogs, and could you go and fish him out? Your manager agrees and away you go. Unfortunately, you get to the bog and there’s no cat! It turns out the peat bog was fixed in your current version. Or at least you think so, because otherwise the cat would be here right? OK so the next step is to install the old version and hunt down this poor cat, except you still need to continue developing your current project, which uses the latest version. Ploppers. You’re snookered! In fact, after a brief investigation you realise the situation is worse than you thought. You’ve upgraded several packages since then, including your language version, and any one of these things could affect the cat and its chances of escaping the peat bog. You silently curse this customer and their vagaries with their cat. What to do? Well, there is one solution that you know about but have been putting off learning for some time. One way, you begrudgingly admit, you can solve this issue is with a virtual environment. A “virtual environment” is an entirely localised install of a language to a particular directory. This install does not interfere with your global installation, and can have its own suite of packages installed independently of your global installation and also of any other virtual environments on the same computer. In other words you can install the older version of your language as well as all the packages you had at the time of distributing the tool to the customer, and their precise versions. This makes it trivial to find the cat and ensure that if you can get it out, the customer will be able too (although the fix may be non-trivial; programming can be like that). Setting up this environment was a right pain to get exactly right, but it really did solve your problems. At this moment you have a minor epiphany and begin to understand why so many developers use virtual environments all the time. If you made a new environment for every project, you’d never have to recreate one. You could just go back to the old environment and begin fixing things straight away. It would be easy. In fact, as you turn it over in your mind a number of benefits become clear. Customers always want to know what they have to install when you give them your scripts, and this has historically been a huge pain in the back. You’ve had to work out all the things you use from the imports in your scripts, and the versions thereof through further analysis of what is installed at that moment. But if every project was in its own virtual environment… You would only ever have to get a list of what was installed. You remember that you’ve seen the termvenv in a few places before this, mainly in PyCharm, the code editor that irritating Python developer (me) won’t shut up about. So you venture forth to do some research about this tool; maybe it can really help you in the future.
Creating Virtual Environments Using venv
That long preamble brings us to here. How you can make your own virtual environments in Python and leverage them in your own development.
There are many virtual environment-creation packages out there, such as pyenv, virtualenv, and conda, but this article will focus on venv, which is part of the Python standard library and will be available to anyone who has Python installed.
Virtual environments (referred to as venvs hereafter for brevity) are simple to create and the basic way of doing it is through the command line/terminal.
In order to run a Python package on the command line we must call python -m example_module, which runs the library module example_module as a script. Some Python packages are built specifically to be run like this (including venv).
venv with the --help flag gives us plenty of information about the tool and how to use it. The simple thing to do is just create a new venv, like so.
my_new_venv with the following contents.
Include/- for files related to C extensions (don’t worry about this one)Lib/- location of packages you install to that venvScripts/- activation scripts & the actual Python executablepyenv.cfg- configuration file about the venv.
Scripts/ contains the activation script.
Calling activate within the directory, will activate your venv and allow you access to the Python within.
pip and such will all work as before. When you’re done, you only need to deactivate it. This can be done with the simple deactivate command.
venv and Visual Studio Code
VS Code actually makes things pretty easy for you. Below I open a simple PyCharm project I created with VS Code and run the script using the Python extension. VS Code automatically detects the venv/ directory and uses that version of Python to run the script! Very neat.
You can actually go a bit further with PyCharm though.
venv and PyCharm
PyCharm goes a step further and locates existing installations of Python on your computer and offers to create a new venv whenever you create a new project. Pretty neat! In fact with PyCharm you don’t have to use the command line at all; it uses venv behind the scenes and happily takes care of everything for you. It’s a great product and my favourite IDE.
Conclusion
In conclusion, virtual environments are a valuable tool to use when developing Python. Their value increases with the amount of development work that you do. I recommend starting with the basics and working your way up. Start withvenv because it is builtin and will always be there for you, and then work your way up to other tools as your experience builds.
TL;DR
- Setting up environments is difficult and sharing them is hard
- Consistent environments are essential for correct deployment of develop and production workspaces
- Putting the effort into creating a virtual environment will reduce the chance of errors when setting up environments
- I recommend learning Python’s builtin
venvpackage first because it will always be there for you.
Useful Articles/Further Reading
- https://towardsdatascience.com/virtual-environments-104c62d48c54#ee81
- https://realpython.com/intro-to-pyenv/#why-not-use-system-python
- https://realpython.com/python-virtual-environments-a-primer/