Locked lesson.
About this lesson
A virtual environment is a helpful tool that allows us to install programs and try out code without affecting the rest of our computer. In this video, you'll learn how to set up your own virtual environment.
Exercise files
Download this lesson’s related exercise files.
Setting up a Virtual Environment.docx57.2 KB Setting up a Virtual Environment - Solution.docx
56 KB
Quick reference
Setting up a Virtual Environment
To use Jupyter Notebooks, we create a virtual environment. Inside that environment, we'll install Jupyter Notebooks 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 data using the command:
mkdir /c/data
To move into that directory, use the command:
cd /c/data
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, in this video we want to set up a virtual environment.
- 00:07 And the virtual environment is sort of a little walled garden inside of your
- 00:11 computer.
- 00:12 It's an area that's sort of set aside from the rest of your computer.
- 00:15 And inside that virtual environment,
- 00:17 we can do all kinds of cool things, we can install different software.
- 00:20 And we can run all kinds of different programs and
- 00:23 it's separate from the rest of our computer.
- 00:25 So if we do something horribly wrong inside the virtual environment,
- 00:28 the rest of the computer's not messed up.
- 00:30 It also allows us to install different versions of things
- 00:34 inside the virtual environment that may be different than
- 00:37 the same thing we might have installed somewhere else on our computer.
- 00:40 So it's just a best practices when it comes to most coding things, and
- 00:45 data analysis is no different.
- 00:46 So first things first,
- 00:48 we need to create a separate directory where we're going to hold all the files
- 00:52 that we're going to be working with throughout the rest of the course.
- 00:55 And we can do that by using the make directory command.
- 00:57 That's just mkdir, and now we want to put this in our C drive, and
- 01:01 let's call this data, let's create a folder called data.
- 01:05 Now, if you're on a Mac or Linux, obviously, you don't have a C drive,
- 01:09 you can just save this to your desktop or anywhere else on your computer.
- 01:13 So now, we need to move into that directory, change directory,
- 01:16 the cd command, and then just type c, and then data.
- 01:20 Now, we can see we're in that directory, or we can type the pwd command to see,
- 01:24 yep, sure enough, there we are in that directory.
- 01:27 We can type the ls command to list the stuff in this directory and
- 01:30 there's nothing listed in there now which we would expect because
- 01:33 we just created this directory.
- 01:35 So I'm going to go ahead and clear this screen, so
- 01:37 let's create our virtual environment.
- 01:38 We have to start by kind of installing it.
- 01:40 And to do that, we type in python -m and then venv, stands for virtual environment.
- 01:46 And now, we want to name this, I'm just going to name ours virtual,
- 01:50 you can name it, anything you want, but it's a virtual environment, so
- 01:52 it kind of makes sense to me to name it virtual.
- 01:55 And it takes a couple of seconds for this to spin up, but it looks like it's done.
- 01:58 Now, we can type the ls command again.
- 02:01 And now, we see there's this virtual directory and
- 02:03 inside of there is all of our virtual environment files.
- 02:06 So we've installed it, now we need to turn it on.
- 02:09 And to do that, we type in source virtual/Scripts with capital S, /activate.
- 02:17 And you'll notice now above our command prompt, we have this little virtual thing,
- 02:22 which means our virtual environment has been turned on.
- 02:24 Now, if you're on Mac or
- 02:25 Linux, the command to turn on your virtual environment is source bin/activate,
- 02:30 slightly different than source virtual scripts activate but sort of the same.
- 02:36 So okay, we've included a screen now and
- 02:38 our virtual environment is still on, we can turn off our virtual environment
- 02:41 anytime we want just by typing in deactivate.
- 02:44 And then you'll notice that little thing above the command prompt where it says
- 02:48 virtual and parentheses is no longer there.
- 02:50 So that's how we know it's been turned off.
- 02:52 So all right, I'm going to go ahead and turn this back on.
- 02:55 And to do that, I can type in that command again,
- 02:57 or I could just type the up arrow key a couple of times on my keyboard
- 03:01 to sort of cycle through the latest commands that I typed in.
- 03:04 And we can see here's that command that turn on our virtual environment, so
- 03:07 I can do that.
- 03:09 And the virtual thing pops back up so
- 03:12 that I know that my virtual environment has been turned on.
- 03:15 So for the rest of the course and for the rest of your life, anytime you want to use
- 03:19 this virtual environment, anytime you want to do data analysis with the tools
- 03:24 that we're going to be using throughout this course, you're going to want to
- 03:27 come to your c/data directory and turn on your virtual environment.
- 03:31 Nothing will really work until that happens,
- 03:33 so you don't always have to be in this c/data directory.
- 03:39 If you created a different project later on and you name it something else and
- 03:43 you put it in the c/JohnElder directory, whatever, then you would obviously
- 03:47 go to that directory from then on and turn on a virtual environment there.
- 03:51 But for us for the rest of this course, we're going to be working in this data
- 03:54 directory inside of this virtual environment, so that's all for this video.
- 03:58 In the next video, we'll go ahead and install Jupyter notebooks and start to
- 04:01 play around with it a little bit, and that'll be coming up in the next video.
Lesson notes are only available for subscribers.