Locked lesson.
About this lesson
To use Python properly, we create a virtual environment. In this lesson, we'll explain how to set one up.
Exercise files
Download this lesson’s related exercise files.
5 - Virtual Environment.docx57.2 KB 5 - Virtual Environment SOLUTION.docx
55.9 KB
Quick reference
Virtual Environment
To use Python properly, we create a virtual environment. Inside that environment, we'll install the 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/python-excel
To move into that directory, use the command:
cd /c/python-excel
To create a virtual environment, use the command:
python -m venv virt
To turn on your virtual environment, use the command:
source virt/Scripts/activate
Hints & tips
- To create a virtutual environment: python -m venv virt
- To turn on a virtual environment: source virt/Scripts/activate
- 00:05 Okay, in this video we're going to set up a virtual environment.
- 00:08 And a virtual environment is a Python coding thing.
- 00:11 Anytime you want to create Python code, you want to set up a Python project,
- 00:15 it's a good idea to set up a virtual environment.
- 00:17 And a virtual environment is sort of like a little walled garden inside of your
- 00:21 computer.
- 00:22 And inside of our virtual environment, we can install different libraries,
- 00:26 different modules, different tools, and
- 00:29 it will sort of keep those things separate from the rest of our computer.
- 00:33 So it's a good idea when you're writing Python code to do that for
- 00:36 a lot of different reasons.
- 00:37 So, one of the main reasons is version control.
- 00:40 From time to time, there are different versions of the tools that you're going to
- 00:44 use in your project that come out.
- 00:46 For instance, Python may have a different version of Python in the future.
- 00:49 If you need to use a specific version of a specific thing,
- 00:52 it's much easier to do that inside of a virtual environment.
- 00:56 And so that's one of the big reasons why we do it.
- 00:58 Another reason is, it contains all of the stuff inside of the virtual environment,
- 01:02 and it sort of keeps it separate from the rest of the computer.
- 01:05 So if we mess something up, it's messed up inside of the virtual environment and
- 01:09 it's contained in there.
- 01:10 And it doesn't sort of spread to the rest of the computer.
- 01:12 So, it's just a good idea to set up a virtual environment whenever you're doing
- 01:16 any sort of Python coding is just really best practices.
- 01:19 So that's what we're going to do in this video and it's really easy to do so.
- 01:22 So I've opened up the Git Bash terminal.
- 01:23 If you don't remember, head over to your Windows start menu and
- 01:27 just type in Git and the Git Bash icon will pop up, just go ahead and
- 01:30 click that and this should appear.
- 01:32 So, you notice at the top, it says codemy@codemy,
- 01:35 that's my Windows login name, so yours is going to be different.
- 01:39 This MINGW64, that's just the Git Bash prompt.
- 01:43 So you can just sort of ignore that.
- 01:45 And right here, this little dollar sign, this is the terminal prompt.
- 01:50 This is where we can type in our commands.
- 01:52 So, the first thing we want to do is create a directory on our computer where
- 01:55 we're going to hold all of our project files.
- 01:57 So, to do that we can type in mkdir stands for make directory, and
- 02:02 I want to put this in the C drive.
- 02:04 And let's put this in a folder called python-excel, okay?
- 02:11 Now, if you are on Mac or Linux obviously there is no C drive so you can just create
- 02:15 it directly anywhere on your computer where you're comfortable putting it.
- 02:18 And the command is the same, mkdir and then name it whatever you want.
- 02:25 So, okay so now we want to move into that directory.
- 02:28 So we do that with the cd command, stands for change directly, and
- 02:33 we just type in that directory we just created so c/python-excel.
- 02:40 Okay, and you'll notice right here, it says we're in that directory.
- 02:43 So now we can type in the ls command if we want to list the stuff that's in this
- 02:47 directory.
- 02:48 And you'll notice there's nothing in there because we just created this directory,
- 02:50 there's nothing in there yet.
- 02:51 So, in the next video we'll go ahead and create our first Python file, but for
- 02:55 now we can just leave it blank.
- 02:58 So now, we want to create our virtual environment.
- 03:01 And to do that we're going to use something called venv, v-e-n-v.
- 03:04 And that just stands for a virtual environment.
- 03:07 And it comes with Python already, so we don't have to actually install it,
- 03:11 we just need to use it.
- 03:12 So to use it, type in python -m, and then venv, and
- 03:16 then let's name our virtual environment.
- 03:20 And I'm going to call ours virt, short for virtual.
- 03:23 So you can name it anything you want, but I suggest you just call it virt.
- 03:27 And go ahead and click Enter, and
- 03:28 this will take a couple of seconds in order to spin up the virtual environment.
- 03:33 And boom, now it's installed.
- 03:35 So we've installed virtual environment, now we need to turn it on.
- 03:38 And now if we type in the ls command to list the stuff in this directory,
- 03:42 you'll notice now there's this virt directory that we just created.
- 03:46 And it's called virt because right here, we called it virt.
- 03:50 If we were to type bob right there, it would be bob right there.
- 03:54 So, virt is the name of it.
- 03:56 So, all of our virtual environment files are located there.
- 04:00 So now we want to turn on our virtual environment,
- 04:05 and to do that we type in source virt/Scripts/activate.
- 04:14 And when we do,
- 04:15 you'll notice now above our terminal prompt is this virt in parenthesis.
- 04:20 And that's what allows us to know our virtue environment has been turned on.
- 04:24 So congratulations if you see this little virt thing on here,
- 04:26 it means you've created and installed and turned on your virtual environment, and
- 04:30 you're good to go.
- 04:31 If you're on a Mac or Linux, this command may not work in which
- 04:37 case try source of bin/activate I think is the Mac command or
- 04:42 /bin/activate, one of those two should work for Mac or Linux.
- 04:48 But for Windows users, it's just source virt/Scripts/activate.
- 04:52 So all right, let's go ahead and clear the screen.
- 04:55 And you'll notice again that virt is still there.
- 04:58 So if you want to turn off your virtual environment,
- 05:01 you can just type in deactivate.
- 05:03 And you'll notice now this little virt is gone above our terminal prompt, so
- 05:07 that means it's been turned off.
- 05:09 So I'm going to press the Up arrow key a few times to cycle through the last
- 05:13 few commands we typed.
- 05:15 So we don't have to re-type this again until I get to this source
- 05:18 virt/Scripts/activate command.
- 05:20 And now I'm just going to hit Enter to turn our virtual environment back on.
- 05:24 And we're ready to go.
- 05:25 So that's all for this video, in the next video, we'll go ahead and
- 05:30 create our first Python program.
Lesson notes are only available for subscribers.