Virtualenv4:54 with Kenneth Love
`virtualenv` is a tool that allows us to segregate our Python projects from each other. In this video, I'll show you how to use Python 3.5's built-in `venv` module to create virtual environments.
The command to create a new virtualenv is
python -m venv <virtualenv name>.
You can refer to the docs for more information about the
Inside of your virtualenv, you'l likely use the
pip commands instead of
So virtual env is a tool that lets us have separate worlds of python libraries for 0:01 separate projects. 0:06 So for instance, 0:07 right now I am in a thing called new project, super duper creative name. 0:08 And so I want to make a new virtual env so that I can install my stuff here for 0:13 new project, so it doesn't conflict with old project. 0:18 Normally when you install something with Pip, it gets installed to 0:22 a global repository of libraries, usually called site packages. 0:25 But what if you do one project with Django 1.8, and 0:30 your next project is with Django 1.9? 0:33 You'd have to make sure that your 1.8 project still works with 0:35 1.9 once you upgrade. 0:39 Or you'd have to decide not to use Django 1.9. 0:41 You'd have to decide not to upgrade and just build everything with Django 0:45 1.8 from now on forever and ever because you never upgrade. 0:49 Or for the best of all the worlds, you use virtual env for 0:53 each of your projects and you just install whatever that project needs. 0:57 Now, back in the days of Python 2, and even the first couple of versions of 1:02 Python 3, you had to install a separate tool to use virtual env. 1:06 So you'd see instructions like pip3 install virtualenv. 1:10 But as of Python 3.3, 1:15 virtual env has actually been merged into the Python standard library. 1:17 So now you can use it without having to install anything. 1:22 This is awesome. 1:25 So how do we create a virtual env? 1:27 So I've gone ahead and I've made a place to do my treehouse Python learning. 1:31 I'm just inside of documents and I've what is it, where am I? 1:35 Whatever. 1:41 Anyway, I'm inside of [LAUGH], you can see I went into documents and 1:42 I just made a folder called, new project. 1:45 You know, maybe you'd wanna do like treehouse, and 1:48 then let's move idle and new project inside treehouse. 1:54 Alright so, maybe you'd want to do that. 2:02 And I'm gonna pretend that I'm about to start on the flash basics course. 2:04 Maybe you're already done with that course, maybe you haven't started it yet 2:09 or you know, or maybe you did it or 2:12 you're going to do it in workspaces and that's a great idea. 2:14 Let's make a directory for our course. 2:17 You do this for each of your projects too. 2:20 Maybe you're building a library website or 2:22 something, you'd make a directory for that. 2:25 So we're going to make a directory for flask basics and we're gonna go in there. 2:27 And now, let's clear my screen. 2:32 Now I need to make the virtual env. 2:35 I can call the virtual env anything I want, but 2:36 generally you want to avoid the names of libraries that you're likely to install, 2:39 or the virtual env module itself, which is actually named VEMV. 2:43 So this is for flash basics, I'm going to use the initials fb. 2:47 So python three dash mvemv. 2:52 And now it expects a name. 2:55 We're gonna put in fb. 2:57 So that dash m flag, this thing right here, 2:59 that actually tells Python to use a particular module. 3:04 Modules have or can have a bit of special code written in them that when they're 3:08 used this way, which the venv module has of course, it knows what to do. 3:12 It has like a special invocation. 3:18 So what this does is it expects the name of a virtual env to create, 3:21 which it got and then it creates a virtual env using that. 3:26 So if we look, we'll see here that we have fb, and 3:29 if we do ls of fb, then we see the virtual env bits and pieces. 3:33 Now we don't want to make our code inside of fb. 3:38 This is where libraries get installed too. 3:42 We don't want to write our own code in there. 3:44 The reason is because at any point, I should be able to just do that. 3:46 And that, and other than installing some packages, I'm right back to normal. 3:50 Nothing's new. 3:57 OK, now, before we can use the virtual env, we have to activate it. 3:58 So we'll do source fb, then activate, and that's it. 4:02 It's activated. 4:09 I know it's activated because I have this little parentheses fb parentheses 4:10 over here, and that says hey you're working in the fb virtual env. 4:15 Which, for 4:19 example if I do which Python you see that it's the Python inside fb bin. 4:20 Right? 4:25 It's not one way on the outside. 4:25 And if I do Python version, it's 3.5. 4:27 So even though it was Python 3 outside of the virtual env, 4:31 inside the virtual env it's just Python. 4:35 And now if I want to install things I can just use pip to install them. 4:38 So we'll do pip install Flask, which is the thing I'm probably gonna need for 4:42 Flask basics. 4:44 And it downloads and installs this stuff. 4:46 And I just go on making code like I normally would, 4:49 just like the course tells me to. 4:52
You need to sign up for Treehouse in order to download course files.Sign up