Loading video player…

Preparing Your Environment

Setting up and activating virtual environments with venv differs between Unix and Windows operating systems.

Creating and activating a virtual environment on a Unix system:

Shell
$ python3 -m venv ./.venv
$ source .venv/bin/activate

Creating and activating a virtual environment on a Windows system: :

Windows Command Prompt
c:\> c:\Python35\python -m venv c:\path\to\.venv
c:\> .venv\Scripts\activate.bat

Here are some resources and additional documentation about setting up virtual environments:

00:00 In this lesson, you will prepare your environment to get it ready for building your Django project. Essentially, there are two things that you need to do to prepare your environment. First, you need to set up a virtual environment, and second, you need to also activate that virtual environment.

00:16 Once you’ve done these two steps, then whoosh! You’re inside of a safe place where you can start installing dependencies, install your Django version, and then start working on your Django project.

00:29 Now, the command for setting up a virtual environment, on a Unix system, is going to be python3 -m venv and then the path to your folder that you want to create.

00:42 So, this is going to be a little bit different if you’re on a Windows machine—make sure to check the description—but essentially the command is going to be the same, just you need to define the path in a slightly different way. And this part on the right side, so after python3 -m venv, this is going to be the path and name to the virtual environment that you want to create. So in this case, I’m creating in the current folder, I’m creating a new folder called .venv that’s going to be the virtual environment.

01:13 Now let’s head over to VS Code and I’m going to show you how to create this.

01:19 So here I’m inside of a folder just called django-setup/ that sits in my Documents/. And right in here, I will create this virtual environment by typing the command that you just saw.

01:31 So I’m saying python3 -m venv. And again, remember I am doing this on a Mac. That’s a Unix system, so if you’re working on Windows it will be a little bit different.

01:43 But here I can say, “Stay in the current folder and then create a new folder called .venv that will be the virtual environment.” So once I press Enter here, I’ve got to wait a little bit.

01:56 And then you see that this .venv/ folder here got created. That means that I successfully created a virtual environment. Now, that doesn’t mean you should start installing Django right away, because first you need to also activate that virtual environment.

02:12 The command for this is going to be—again, on a Unix system, a little different than on Windows. On Unix, you can say source and then give the path to the activate script.

02:23 That’s inside of the name of the virtual environment that I created, then the bin/ folder, and then there sits an activate script.

02:30 I can show you this in VS Code over there but if you’re on Windows, it will be a little bit different. You need to give the path to your virtual environment and then into a Scripts\ folder, and then there’s a file called activate.bat.

02:44 You want to execute that file instead.

02:47 Let’s look at this on macOS. I will show you where the file is. So inside of .venv/ and then the bin/ folder, you can see there’s an activate script.

02:58 No need to understand in detail what’s going on there. If you want to learn more about virtual environments, make sure to check out the course that we have on that too.

03:07 By now, I’m ready to activate it. I’m going to say source ./.venv/bin/activate and I press Enter.

03:17 And once you see this name of your virtual environment appear here, so I can see (.venv) showing up in front of my prompt, that means that I successfully activated the virtual environment and now I’m ready to start installing dependencies if I want to.

03:33 As a note, again, you always want to look for the name of your virtual environment before your prompt just to confirm that you’ve actually activated it and that you know that now you’re in a safe space and you can continue with the next steps.

03:49 As a quick recap, to prepare your environment you need to do two things. First, set up a virtual environment, and then also activate that virtual environment.

03:57 And if this whole concept of virtual environments is a little blurry to you still, then make sure to check out the dedicated tutorial and course that we have on this. All right! Next up, you can get ready to install Django and pin your dependencies.

Avatar image for marcinszydlowski1984

marcinszydlowski1984 on July 6, 2022

Good explanation but it’s worth to mention that it’s not necessary to enter additional “./” during create a virtual environment. python3 -m venv .venv is enough.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on July 6, 2022

@marcinszydlowski1984 Technically, you don’t even need the leading dot, as this will work too:

$ python3 -m venv venv

Personally, I like to include the trailing forward slash to remind myself and others that the second argument is a path or the folder name:

$ python3 -m venv venv/

Become a Member to join the conversation.