Using Virtual Environments in Python: A Beginner’s Guide

Brian Zhang
3 min readMay 1, 2021

--

In order to dive into more advanced Python projects, such as machine learning, natural language processing or quantum computing, you’ll need to install packages. Python’s standard library is already very extensive, but by adding packages (or modules), you can extend the functionality of your programs even further. In order to install the packages neatly, most programmers use what is called a virtual environment to keep everything organized.

Imagine a suitcase full of tools that you can use for a project. Every project needs different tools, so it makes sense to have different suitcases for each project. Without this, it’d be easy to confuse different versions of the same tool, and you’d have to lug a suitcase full of every tool you’ve ever used around. In essence, virtual environments are the multiple suitcases that programmers use to organize their tools, or packages.

Using a different virtual environment lets you pick and choose which packages you install for each project. This lets you add only the packages you need for one project, rather than having all of them installed in a big “suitcase”. Some packages are also finicky and only work with specific versions of other packages, which is easy to manage if you’re in a separate virtual environment but a massive pain otherwise.

There are many ways to install packages, but I use the PyCharm IDE, developed by Jetbrains. PyCharm is fairly straightforward to use and has several features including automatic GitHub integration. You can download it from the JetBrains website: https://www.jetbrains.com/pycharm/download/. The premium version of PyCharm is also available for free if you’re a student, which you can read more about here: https://www.jetbrains.com/community/education/#students.

Either way, when you create a new project using PyCharm, it can automatically create a new virtual environment, abbreviated as venv, in the project creation screen:

PyCharm makes virtual environments automatically when you make a new project

Or you can add one to an already existing Python project by going to File → Settings → Project → Python Interpreter → Add

Adding a virtual environment to a pre-existing Python project

You can then make your new virtual environment or use an already-existing one.

Creating a new virtual environment

Either way, you should now see a (venv) symbol in front of your current line in the PyCharm terminal:

From here, you can install new packages using pip freely without worrying about messing up your other projects. I hope this helps you on whatever project you work on next!

If you liked this article, consider looking at my website. You can also find me on LinkedIn here.

--

--

Brian Zhang
Brian Zhang

Written by Brian Zhang

Student looking to get into programming. Website: https://bbrianzzhang.github.io/

No responses yet