- HD
- 720p
- 540p
- 360p
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
In this lesson, you'll learn how to setup a Virtual Environment so you can run Django projects and commands without affecting the rest of your computer.
Exercise files
Download this lesson’s related exercise files.
Set Up A Virtual Environment57.2 KB Set Up A Virtual Environment - Solution
57.7 KB
Quick reference
Set Up A Virtual Environment
To use Django, we create a virtual environment. Inside that environment, we'll install Django and other tools that we'll need.
When to use
Set up a virtual environment once per project. Then, every time you work on your project, make sure to turn on your virtual environment.
Instructions
First, create a new directory called mywebsite using the command:
mkdir /c/mywebsite
To move into that directory, use the command:
cd /c/mywebsite
To create a virtual environment, use the command:
python -m venv virtual
To turn on your virtual environment, use the command:
source virtual/Scripts/activate
Hints & tips
- To create a virtutual environment: python -m venv virtual
- To turn on a virtual environment: source virtual/Scripts/activate
- 00:04 Okay, so we've got all the tools that we need to start building out our project.
- 00:08 The next thing we need to do is create a virtual environment.
- 00:11 And think of a virtual environment as a walled garden, it's a little space inside
- 00:15 of your computer that's walled off from the rest of your computer.
- 00:19 And inside of our virtual environment, we can create our Django project.
- 00:22 Now, it's a good idea to create sort of a separate area in your computer,
- 00:26 the virtual environment, whenever you create Django projects,
- 00:29 often when you create almost any kind of project and coding.
- 00:32 Because if something goes wrong with your project if something gets out of hand,
- 00:36 the rest of your computer isn't affected.
- 00:38 It also allows us to install different things inside of the virtual environment
- 00:43 that we might not want installed on our computer in general, so for instance,
- 00:48 Django.
- 00:48 Now, you might have ten Django projects, and
- 00:51 they all might require a different version of Django.
- 00:53 If you created a website ten years ago using Django,
- 00:56 that website's going to use a very old version of Django.
- 01:00 So you might not necessarily want to install that old version of Django on your
- 01:04 entire computer, but you would want to install it
- 01:06 inside the virtual environment for that specific project.
- 01:10 So it's a good way to keep things separate.
- 01:12 And it's just sort of best practices when it comes to coding, especially Django.
- 01:16 So that's what we're going to do in this video.
- 01:18 So I've opened up my Git Bash terminal, and the first thing we want to do is
- 01:21 create a directory where we want to put our new Django project.
- 01:24 And to do that we just type in mkdir, stands for make directory,
- 01:29 and we want to put this in our C drive.
- 01:32 And what do we want to call this?
- 01:33 Let's just call this my website.
- 01:35 We're going to build a website.
- 01:36 Let's just call the directory my website.
- 01:38 All right, so now it's been created.
- 01:40 Now we need to move into that directory.
- 01:42 So we type the cd command, stands for change directory,
- 01:47 and then just type in that directory /c/mywebsite.
- 01:51 Hit Enter, and you see right here, now it says we're in that directory.
- 01:55 Or you could type in pwd, and that also shows you what directory you're in.
- 02:00 So if we type in ls to list the stuff in our directory, nothing appears.
- 02:05 And that's because there's nothing in our directory yet.
- 02:07 So I'm going to type clear to clear the screen.
- 02:10 So let's go ahead and create our virtual environment.
- 02:13 And to do that, we just type in python -m and then we want to create
- 02:17 a virtual environment, a venv, stands for virtual environment.
- 02:22 And now we just need to name the virtual environment.
- 02:24 What do we want to call this thing?
- 02:26 Well, I'm just going to call it virtual.
- 02:29 And it takes a second, and then it's done.
- 02:32 So now, if we type in ls to list the stuff in this directory,
- 02:37 we see there's this virtual folder.
- 02:40 So that means our virtual environment has been created successfully.
- 02:43 So we've created it, now we need to turn it on.
- 02:46 And you'll need to do this every time you work with your Django Project.
- 02:49 Anytime you want to do anything at all, you want to come in here,
- 02:53 moving to your c/mywebsite directory and then turn on your virtual environment.
- 02:59 So, to turn it on we type in source and then the directory we just created,
- 03:06 that's virtual/ and then Scripts and then activate.
- 03:11 So source virtual, virtual is just the name of our virtual environment.
- 03:17 And then inside of there, there's a Scripts directory and an activate file.
- 03:21 So we do that and then boom, we see this virtual in parentheses appears.
- 03:25 So now if I hit Enter a bunch of times, every time I do,
- 03:29 this virtual appears above the command prompt, all right?
- 03:33 If we clear the screen, again, it appears.
- 03:36 So that tells us that our virtual environment has been turned on, right?
- 03:40 So that's how you do it for Windows.
- 03:42 If you're on a Mac and you're using something other than the Git Bash
- 03:45 terminal, Mac and Linux computers come with their own terminal.
- 03:48 So you don't necessarily have to use this one,
- 03:51 you still can because there's a Mac version of the Git Bash terminal, and
- 03:54 I suggest you do so that you can follow along and use the same commands that I do.
- 03:58 But if you're using the terminal that came with your Mac or Linux,
- 04:03 then the command to turn on your virtual environment is source bin/activate.
- 04:09 For Windows users, you just use the command that I did just a second ago.
- 04:14 Mac users, you'll use that source bin/activate.
- 04:16 Now, to turn off your virtual environment, you can just go ahead and
- 04:19 close your Git Bash terminal.
- 04:21 That will turn it off, or you can type in deactivate.
- 04:27 And when you do, you'll notice this virtual in parenthesis has disappeared.
- 04:31 If I hit Enter a bunch of times, it doesn't appear above it anywhere,
- 04:35 all right?
- 04:36 So then to turn it back on again, we just type source virtual/Scripts/activate.
- 04:42 And I've just pressed the up arrow key on my keyboard
- 04:45 to cycle through the most recent commands.
- 04:48 So you can do that, it's kind of a shortcut.
- 04:50 And when you do boom, now that virtual appears right there and we're good to go.
- 04:56 So that's all there is to virtual environment.
- 04:58 In the next video, we'll actually install and start using Django.
Lesson notes are only available for subscribers.