1 00:00:01,218 --> 00:00:06,790 So virtual env is a tool that lets us have separate worlds of python libraries for 2 00:00:06,790 --> 00:00:07,800 separate projects. 3 00:00:07,800 --> 00:00:08,730 So for instance, 4 00:00:08,730 --> 00:00:13,900 right now I am in a thing called new project, super duper creative name. 5 00:00:13,900 --> 00:00:18,660 And so I want to make a new virtual env so that I can install my stuff here for 6 00:00:18,660 --> 00:00:21,460 new project, so it doesn't conflict with old project. 7 00:00:22,550 --> 00:00:25,500 Normally when you install something with Pip, it gets installed to 8 00:00:25,500 --> 00:00:30,130 a global repository of libraries, usually called site packages. 9 00:00:30,130 --> 00:00:33,214 But what if you do one project with Django 1.8, and 10 00:00:33,214 --> 00:00:35,230 your next project is with Django 1.9? 11 00:00:35,230 --> 00:00:39,815 You'd have to make sure that your 1.8 project still works with 12 00:00:39,815 --> 00:00:41,900 1.9 once you upgrade. 13 00:00:41,900 --> 00:00:45,210 Or you'd have to decide not to use Django 1.9. 14 00:00:45,210 --> 00:00:49,858 You'd have to decide not to upgrade and just build everything with Django 15 00:00:49,858 --> 00:00:53,390 1.8 from now on forever and ever because you never upgrade. 16 00:00:53,390 --> 00:00:57,673 Or for the best of all the worlds, you use virtual env for 17 00:00:57,673 --> 00:01:01,100 each of your projects and you just install whatever that project needs. 18 00:01:02,560 --> 00:01:06,240 Now, back in the days of Python 2, and even the first couple of versions of 19 00:01:06,240 --> 00:01:10,630 Python 3, you had to install a separate tool to use virtual env. 20 00:01:10,630 --> 00:01:14,060 So you'd see instructions like pip3 install virtualenv. 21 00:01:15,670 --> 00:01:17,917 But as of Python 3.3, 22 00:01:17,917 --> 00:01:22,980 virtual env has actually been merged into the Python standard library. 23 00:01:22,980 --> 00:01:25,990 So now you can use it without having to install anything. 24 00:01:25,990 --> 00:01:27,930 This is awesome. 25 00:01:27,930 --> 00:01:30,065 So how do we create a virtual env? 26 00:01:31,090 --> 00:01:35,290 So I've gone ahead and I've made a place to do my treehouse Python learning. 27 00:01:35,290 --> 00:01:41,990 I'm just inside of documents and I've what is it, where am I? 28 00:01:41,990 --> 00:01:42,820 Whatever. 29 00:01:42,820 --> 00:01:45,400 Anyway, I'm inside of [LAUGH], you can see I went into documents and 30 00:01:45,400 --> 00:01:47,140 I just made a folder called, new project. 31 00:01:48,290 --> 00:01:54,000 You know, maybe you'd wanna do like treehouse, and 32 00:01:54,000 --> 00:01:59,220 then let's move idle and new project inside treehouse. 33 00:02:02,140 --> 00:02:03,690 Alright so, maybe you'd want to do that. 34 00:02:04,690 --> 00:02:09,640 And I'm gonna pretend that I'm about to start on the flash basics course. 35 00:02:09,640 --> 00:02:12,700 Maybe you're already done with that course, maybe you haven't started it yet 36 00:02:12,700 --> 00:02:14,460 or you know, or maybe you did it or 37 00:02:14,460 --> 00:02:17,990 you're going to do it in workspaces and that's a great idea. 38 00:02:17,990 --> 00:02:20,590 Let's make a directory for our course. 39 00:02:20,590 --> 00:02:22,880 You do this for each of your projects too. 40 00:02:22,880 --> 00:02:25,330 Maybe you're building a library website or 41 00:02:25,330 --> 00:02:27,270 something, you'd make a directory for that. 42 00:02:27,270 --> 00:02:32,810 So we're going to make a directory for flask basics and we're gonna go in there. 43 00:02:32,810 --> 00:02:35,240 And now, let's clear my screen. 44 00:02:35,240 --> 00:02:36,961 Now I need to make the virtual env. 45 00:02:36,961 --> 00:02:39,407 I can call the virtual env anything I want, but 46 00:02:39,407 --> 00:02:43,802 generally you want to avoid the names of libraries that you're likely to install, 47 00:02:43,802 --> 00:02:47,390 or the virtual env module itself, which is actually named VENV. 48 00:02:47,390 --> 00:02:52,690 So this is for flash basics, I'm going to use the initials fb. 49 00:02:52,690 --> 00:02:55,990 So python3 -m venv, 50 00:02:55,990 --> 00:02:57,050 and now it expects a name. 51 00:02:57,050 --> 00:02:58,440 We're gonna put in fb. 52 00:02:59,690 --> 00:03:03,470 So that -m flag, this thing right here, 53 00:03:04,890 --> 00:03:08,210 that actually tells Python to use a particular module. 54 00:03:08,210 --> 00:03:12,710 Modules have or can have a bit of special code written in them that when they're 55 00:03:12,710 --> 00:03:18,940 used this way, which the venv module has of course, it knows what to do. 56 00:03:18,940 --> 00:03:21,560 It has like a special invocation. 57 00:03:21,560 --> 00:03:26,030 So what this does is it expects the name of a virtual env to create, 58 00:03:26,030 --> 00:03:29,570 which it got and then it creates a virtual env using that. 59 00:03:29,570 --> 00:03:33,130 So if we look, we'll see here that we have fb, and 60 00:03:33,130 --> 00:03:38,950 if we do ls of fb, then we see the virtual env bits and pieces. 61 00:03:38,950 --> 00:03:42,050 Now we don't want to make our code inside of fb. 62 00:03:42,050 --> 00:03:44,370 This is where libraries get installed too. 63 00:03:44,370 --> 00:03:46,380 We don't want to write our own code in there. 64 00:03:46,380 --> 00:03:50,790 The reason is because at any point, I should be able to just do that. 65 00:03:50,790 --> 00:03:57,110 And that, and other than installing some packages, I'm right back to normal. 66 00:03:57,110 --> 00:03:57,780 Nothing's new. 67 00:03:58,835 --> 00:04:02,950 OK, now, before we can use the virtual env, we have to activate it. 68 00:04:02,950 --> 00:04:09,770 So we'll do source fb/ bin/activate, and that's it. 69 00:04:09,770 --> 00:04:10,980 It's activated. 70 00:04:10,980 --> 00:04:15,090 I know it's activated because I have this little parentheses fb parentheses 71 00:04:15,090 --> 00:04:19,600 over here, and that says hey you're working in the fb virtual env. 72 00:04:19,600 --> 00:04:20,110 Which, for 73 00:04:20,110 --> 00:04:25,130 example if I do which Python you see that it's the Python inside fb bin. 74 00:04:25,130 --> 00:04:25,750 Right? 75 00:04:25,750 --> 00:04:27,540 It's not one way on the outside. 76 00:04:27,540 --> 00:04:31,970 And if I do Python version, it's 3.5. 77 00:04:31,970 --> 00:04:35,218 So even though it was Python 3 outside of the virtual env, 78 00:04:35,218 --> 00:04:38,420 inside the virtual env it's just Python. 79 00:04:38,420 --> 00:04:42,160 And now if I want to install things I can just use pip to install them. 80 00:04:42,160 --> 00:04:44,950 So we'll do pip install Flask, which is the thing I'm probably gonna need for 81 00:04:44,950 --> 00:04:46,600 Flask basics. 82 00:04:46,600 --> 00:04:49,080 And it downloads and installs this stuff. 83 00:04:49,080 --> 00:04:52,410 And I just go on making code like I normally would, 84 00:04:52,410 --> 00:04:53,490 just like the course tells me to.