The Simplest Way to Create a Python Virtual Environment

09 Mar · by Tim Kamanin · 2 min read

A lot of fog and mysteries surround Python virtual environments. There are loads of packages, tools, and wrappers trying to make developer's life with virtual environments easier.

But if we look at the basics, we'll see that actually, creating a virtual environment in Python has never been easier.

Starting from Python 3.3, you can create a virtual environment by simply running the following command in a terminal:

bash
python -m venv myblog

That's all! You've just created a Python virtual environment!

Behind the scenes, the command has created the myblog virtual environment and the directory with the same name at your current location.

Now you can activate myblog environment by running:

bash
source myblog/bin/activate

When a virtual environment is activated, any command you run in the current terminal session will use Python executable and packages installed under the myblog environment.

So if you run a pip install command like this:

bash
pip install Django

It will install Django into the myblog virtual environment. Neat, huh?

How to deactivate a virtual environment?

If you want to leave the previously activated environment, run the following command in a terminal:

bash
deactivate

Alternatively, you could close the terminal window and achieve the same effect.

Now you know that Python virtual environment isn't a mystery anymore. So go and create one!

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials