What is virtual environment? (using Node.js terminology)

For people who are familiar with Node.js development environment, virtual environment is somewhat similar. In Node.js, for your specific project, all the modules you installed is tailored specific to your project. It doesn’t affect anything outside of that project unless you npm install the module with -g tag. We have something similar to Python development environment as well. It is called virtual environment. It is essentially a place where you can pip install any modules and knowing that anything you do in this environment isn’t going to affect anything out there.

How Python virtual environment can be useful?

This is super useful since people can experinement anything they want inside this isolated area. Once they are satisfy with the modules that are installed, simple run pip freeze and port its result to a requiement text file so that others can have exactly the same development environment as you do. This is especially helpful in a team setting where group of people working on the same project together. On the other hand, for someone who has multiple Python project on their local computer may need different version of library, this virtual environment also comes in handy for that as well. In the following, we will be showing on how one can activate virtual environment in PowerShell.

How to activate Python virtual environment in PowerShell

Assume that you already have the virtual environment set up and you are wondering how one can enter the virtual environment in PowerShell. It is actually pretty simple, simply run the follow command.

.\.venv\Scripts\Activate

source code hosted on GitHub

Once we are in the virtual environment, it will be indicated on the left side. If we try import a library that we installed only in this virtual environment, we will be able to. virtual env

If we try to import it anywhere else where we don’t have the library install. This will happen, without virtual env

What Happens in Vegas, Stays in Vegas

Same thing will happen to the library installed in the virtual environment.

Tada! Now that you are in the virtual environment, start coding!!!

Wrapping Up

Hopefully this article has help you understand what a virtual environment, how it can be useful, and how to activate it and thank you for reading!

Resources

I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started