A Simple Introduction to Python Virtual Environments
A crucial next step for budding Pythonistas
It’s natural to focus — and get stuck on — syntax, structures, and libraries when learning a new programming language. Especially if it’s your first language, learning how to manage an environment is normally filed away under blissful ignorance.
It’s possible (somewhat) to get quite far without tackling environment management, but once you hit a wall that requires it, you’ll be left looking at a disorganized system folder that can best be described as a cluster****.
To start building good habits early, introduce Python virtual environments. We’ll explain the challenges that virtual environments address and how to get started using them — it’s really easy, by the way.
The ‘What’ and ‘Why’ of Virtual Environments
Virtual environments are essentially what they sound like — virtualized spaces that are exempt from the world around them. What this means is that all the noise of the outside world is blocked while inside a virtual environment.
Imagine the following scenario. We’re learning Python and going crazy with pip install. We grab anything and everything, downloading the latest stable releases. Eventually, we find a tutorial to follow, but it requires a lower version an already installed package. But our previous sample code used these cool new features of the latest build…we’re at an impasse. 😤
This scenario could have been avoided entirely by organizing our workspace with Python virtual environments. Packages are installed locally to the virtual environment, circumventing conflicts.
Getting Started with Virtual Environments
Aside from the actual benefits of virtual environments, one of their best features is the ease of use. Minimal effort is required to begin using virtual environments. In fact, the hardest part is building the habit to use them, which is why we advocate learning early.
We’re going to assume we’re working with Python 3+, so virtual environments are natively available without additional download. To get started, let’s make a new directory called my_python_venvs and enter it.mkdir my_python_venvs
cd my_python_venvs
This directory can be considered your parent directory for virtual environments. Now, use the following command to create our virtual environment.python3 -m venv my_venv
The command executes the module venv and passes the name of our to-be created environment. You won’t see any noticeable feedback on the screen, but a simple directory listing will show the new directory holding our virtual environment.
Next, we need to activate our virtual environment, which is effectively saying “start working in this environment”. Otherwise, Python will use the global settings despite being in the directory of our virtual environment.cd my_venv
source bin/activate
At this point, we should see the name of our virtual environment in parentheses at the beginning of our command prompt. That’s it!
Let’s poke around a bit. Step into the /bin directory and we’ll see the activate script that we just executed as well as python and pip. These are the modules available in our environment. Try installing a package and list the directory again, it will be installed locally.
Finally, to stop working within the virtual environment, use the following one-word command.deactivate
We’ll see our command prompt return to normal and our environment will be waiting for us the next time we activate it.
Conclusion
It’s easy to get caught up in eagerness every time a new snippet or project comes up, but get used to pumping the brakes and spinning up a virtual environment first.
Remember, Python virtual environments are native to Python3+ and able to be created with a single command. You have to activate the environment to utilize its virtual boundaries. While active, your packages will be self-contained, avoiding conflicts with other projects and package installations.
Please share your experiences, questions, and feedback below. Follow Code 85 for more plain language programming guides. Thanks for reading!