Loading video player…

Creating a Virtual Environment

This lesson covers how to create a virtual environment in a project folder. You learned that following the steps below will install a self contained Python environment in your project directory:

  • Create a project directory
  • Change into the project directory
  • Run python3 -m venv <name_of_virtualenv>

00:00 So, first of all, we’re going to take a look at where the global environment currently lives, and we can do that with the which command. In this case, I’m going to be using Python 3, which means that I’m going to add this 3 suffix to all of my commands here, like pip3, python3, and so on. All right, so the first thing that we’re going to do is we’re going to take a look at where the global environment currently lives.

00:25 And I’m going to do that with the which command, which tells me the path to a file or to an executable. And in this case, I’m looking at the pip3 command because I’m using Python 3 here.

00:37 So if I run this, it tells me that my Python install lives inside /usr/local/bin/—or, my pip install lives inside /usr/local/bin/. This is typical if you’ve installed that through homebrew on macOS, for example.

00:50 Now, I’m going to pretend I’ll be working on a new Python project here, so I’m going to create a new directory for this project, and then I’m going to change into this project directory.

01:02 And what I’m going to do now is I’m going to create a virtual environment inside this folder here. So, what I’m going to do here, I’m going to use the magic incantation python3 -m venv, which stands for virtual environment, and I’m just going to tell it to create a virtual environment inside this my-python-project/ folder, and I want it to create that inside a new subdirectory called venv, which is just a naming convention that I like to use.

01:30 And this is going to set up a new virtual environment. This should finish any minute now. All right, so we just created the new virtual environment, and now when I look at the contents of my project folder, previously it was empty and now I’ve got this venv/ folder in there, and then when we look inside it, you can see there’s a bunch more directories and pretty much, like, a full Python install. And we can drill down further just to show you that there’s a bunch of stuff in this folder here!

02:00 So, when we just list a directory tree here, you can see that we’ve got this bin/ folder, this include/ folder, lib/ folder.

02:09 This is where all of our Python third-party packages are going to live, and they were seeded with some basic stuff, like a very basic Python install. And that’s what a virtual environment does. Now we’ve pretty much got this tiny self-contained virtual environment—a Python environment—installed inside our project folder.

Avatar image for Charlie Grover

Charlie Grover on Sept. 11, 2021

I cannot activate using the responses up top of this post. I am running Python 3.9

In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:

$ source env/bin/activate (env) $

I have tried every combination of the python line to no avail. please help.

Avatar image for Martin Breuss

Martin Breuss RP Team on Sept. 13, 2021

Hi @Charlie Grover, what operating system are you working on?

Check out the different commands for activating your venv described in the official Python documentation.

Avatar image for h602421365

h602421365 on Dec. 11, 2021

I use the command prompt and it says that which is not recognized

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Dec. 13, 2021

@h602421365 which is a shell command available on Unix-like operating systems, including macOS and Linux distributions like Ubuntu. If you’re on Windows, then you might try the equivalent where command.

Avatar image for VitaminC

VitaminC on Oct. 5, 2022

Does Windows have an equivalent to the ‘which’ command?

Avatar image for VitaminC

VitaminC on Oct. 5, 2022

Forget question above - found answer on a later page discussion.

Avatar image for Ranga

Ranga on April 3, 2023

For windows, the command to check python source folder is

pip3 --version.

python --version

only prints out the version of the global environment python, whereas

pip3 --version

prints out the folder in which pip is present.

To list down the folder structure after installation of the venv, try using the command

tree .

This will show the current folder and the installed venv folders and files.

Avatar image for Ranga

Ranga on April 3, 2023

A correction to my earlier comment.

tree.

will show only the folder structure.

Using

tree /f .

will show both folders and files.

Avatar image for Laszlo Laszlo

Laszlo Laszlo on Aug. 7, 2025

Hello,

I think pip is a little bit too mature :) Works fine but take a look at uv. It is able to handle multiple Python versions and virtual environments.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 7, 2025

Hey Laszlo, and thanks for you comment! It’s true that pip is mature, but it still has its place. If you want to learn more about uv, then check out our dedicated tutorial: Managing Python Projects With uv: An All-in-One Solution. We’re working on another tutorial that compares uv vs pip, too 😉

Become a Member to join the conversation.