Virtualenv6:05 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.
Virtual ENV is a tool that lets us have separate worlds of Python libraries for 0:00 separate projects. 0:04 Normally when you install something with PIP, it gets installed to 0:06 a global repository of libraries that we usually call site packages, but 0:09 one if you do one project with Jango 1.8 and your next project is with Jango 1.9? 0:14 You'd have to make sure that your 1.8 project still works with 1.9 once you 0:19 upgrade, or you'd have to decide not to upgrade and just build everything 0:23 with Jango 1.8 from now, forever and ever, just from now on or 0:29 for the best of all the worlds, both the worlds, whatever, you could use virtual 0:33 envy for each project and just install whatever that particular project needs. 0:38 Now back in the days of Python two, and even the first two versions of Python 0:44 three, you had to install a separate tool to use virtual ENV. 0:48 In Python three point three, virtual ENV was moved into the Python standard 0:52 library, so we can use it without having to install anything, yay. 0:56 So, how do we create a virtual ENV? 1:01 Well, first let's make a place to do all of our Treehouse Python learning. 1:02 I'm gonna do mkdir, Treehouse, and 1:07 then I'm going to go into that Treehouse directory, and 1:12 I'm going to pretend that I'm about to start on the flask basics course. 1:16 You might already be done with this course or you might not have even started it yet. 1:21 Let's make a directory for the course. 1:25 You would do this for each project you are working on too. 1:27 So maybe you're building a library website or something. 1:31 You'd make a directory for it. 1:33 But for now, though, we're going to do mkdir and 1:34 I'm going to call it flask_basics. 1:37 And then I'm going to cd into there. 1:40 And now I need to make the virtual ENV. 1:44 I can call the virtual ENV anything that I want, but generally, 1:47 you want to avoid the names of libraries that you're likely to install, like flask. 1:50 Or the virtual ENV module itself, which is named VENV. 1:55 Now, since this is for Flask basics, I'm just going to use the initials FB. 2:00 So we do, python -m, venv, and 2:05 then the name that we're going to use, which is fb. 2:08 The -m flag, is a flag that tells Python to use a particular module. 2:13 Modules have to have a bit of special code written in them to be used in this way, 2:20 and the Virtual V and V module has it, obviously. 2:25 So when we tell Python which library to use, 2:29 which is the virtual ENV, And virtual env then expects to receive a name for 2:32 the Virtual env that it's going to make, and we called it FB. 2:38 Now if we check the directory with dir 2:42 then we will see that we have this FB right here. 2:45 And FB is the directory that has all of our virtual env tools in it. 2:49 That is our virtualenv. 2:55 That is where our libraries are gonna be installed. 2:56 Now, a lot of people think, okay, I'm going to write my code in there, but 2:59 you don't want to write your code in there. 3:02 Virtual envs are meant to be disposable. 3:04 Sometimes, something goes wrong, and you need to get rid of your virtual env, 3:06 you should be able to just delete it, make a new one, and 3:10 nothing seems like it has changed at all. 3:14 Now before we can use the virtual env we have to activate it. 3:17 Sadly, this is different depending on whether you use the Windows Command shell 3:20 or PowerShell. 3:24 I'm going to cover both of them and I'm going to start with the Command shell 3:25 because it's easiest, and because I already have it open. 3:28 So I would do fb slash Scripts with a capital S 3:31 slash activate dot bat, press Enter and that's it. 3:36 The virtual ENV is now activated and 3:41 you can tell that by having this FB thing right over here. 3:44 So cool, that was easy. 3:48 So now let's try this in Powershell. 3:51 So we have to launch Powershell as an administrator, so 3:53 I pull it up and I right click and I choose run as administrator. 3:58 Yes. 4:03 It can make changes. 4:04 And so once this gets up and running, PowerShell has a security policy in place 4:06 called the execution policy, which doesn't allow remotely signed software to be run, 4:11 or unsigned software for that matter. 4:16 So it requires a valid local signature in order to run the software. 4:18 And our software doesn't currently have that. 4:23 Virtually env at python itself are signed remotely, so 4:26 we can't use them without changing this. 4:29 This does make your windows slightly less secure. 4:32 So if you don't want to do it, feel free to just use the standard command prompt. 4:35 But we're going to run set- execution policy, and 4:39 we're going to set that to remote signed and we press enter. 4:44 And it asks us like are you sure you want to change this. 4:49 And we're going to say yes. 4:52 And it's changed. 4:54 Cool. 4:57 So now we can close this, we don't need to run the administrator power shell anymore. 4:57 We can just do the regular power shell, so here we are and 5:02 let's go back into our treehouse directory and our flask basics directory. 5:06 Okay, so we're back into our project and 5:13 now we can activate our virtual env and it's done a little bit differently. 5:16 We do fv/scripts /activate.ps1. 5:19 It's power shell, so PS. 5:26 Kind of makes sense there, right? 5:30 And once again, we have the FB thing, right here on our shell. 5:32 Now, no matter which shell you're using, whether you're using power shell or 5:35 the Windows command prompt, the rest of these steps are the same. 5:38 I'm going to do them in PowerShell since I'm already in it. 5:41 So first we're going to install Flask into 5:44 our virtualenv, so pip install flask. 5:50 And it collects all the things it needs, downloads and installs everything. 5:55 And then we would just go about making files like we normally would 5:59 following the instructions in the course. 6:03
You need to sign up for Treehouse in order to download course files.Sign up