Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
IDLE in a Virtual Environment4:08 with Kenneth Love
Using tools like IDLE in a virtualenv isn't as straightforward as it should be. I'll show you how to launch it so your custom project packages are available and how to use a requirements file to quickly build up an environment.
You can always get a list of installed packages with
pip freeze. If you're in a virtualenv, you'll get the virtualenv's list of packages. If you're not, you'll get a list of your global packages.
pip freeze > requirements.txt is the fast way to generate your requirements file the first time.
Requirements files always have the same layout:
package_name==1.2.3. You can also use
>=, too (e.g.
Django>=1.9,<2.0 would select the latest version of Django that's newer than, or equal to, 1.9 and less than 2.0).
So I want to show something special here. 0:01 If I open up Idle, let's just go find it again, like we did before. 0:03 All apps, Idle, there we go. 0:09 Okay so I installed flask but here if I do import flask, I can't get to it, 0:10 because I installed it in a virtualenv and I didn't install it globally. 0:17 So if I want to use Flask to write and run my software that is 0:21 inside a virtualenv, I have to launch idle in a special way. 0:27 So we do python-m, 0:31 because again we're gonna use a special module, and we do idlelib. 0:33 And, so now we've got this. 0:39 And If I do import flask, then no errors because it finds flask. 0:40 All right. 0:46 So, that's important to know. 0:47 Inside of a virtualenv is different from outside the virtualenv. 0:49 So what if our virtualenv gets all screwed up and we need to start over? 0:53 I said in the last video that these were meant to be disposable, so 0:58 let's dispose of this one. 1:01 Before we do that though, let's get a list of the things we have installed so 1:03 that we can install those again. 1:06 We do that using the pip command of freeze. 1:08 So pip freeze tells us we have these five packages installed. 1:12 And you'll see that there's the package name on the left 1:16 flask it's dangerous jinja two etcetera. 1:19 And then the stable equal means install exactly this version and 1:21 then on the right we have the version to exactly install so, we want flask version 1:25 zero point ten point one not flask 0.10.0 or 0.10.2 or 0.5.2 or anything like that. 1:30 We want exactly 0.10.1. 1:34 Now we could copy and paste that. 1:35 I don't like either. 1:37 I'm kinda lazy. 1:39 I don't wanna copy and paste that into a file. 1:40 So let's do something computer do at force. 1:47 Computers are good at doing things, right? 1:51 All right. So we do pit freeze again. 1:53 But before we press enter, we're gonna do a greater than symbol, and 1:56 then we'll give it a file name. 2:00 And we're gonna say requirements which I can't type. 2:03 Requirements.txt and we're gonna press return. 2:07 And if we do a dir we see that there's a requirements file in here right now. 2:12 And, if we cat it, then it prints out the same thing as we did print prix before. 2:17 So, the requirements .txt file name isn't a requirement [LAUGH], 2:24 but, it is a common practice and some third party services like Heroku, for 2:28 example, expect that file name. 2:33 Alright so let's trash, 2:35 let's clear our screen, let's trash this virtual environment and make a new one. 2:38 So, I'm still in it, I'm still activated, so let's get out of it. 2:43 Let's deactivate and let's get rid of it. 2:47 So, the way we get rid of a directory is we do RM, and yes, 2:52 I want to delete that, alright, and now I'm starting over. 2:56 So I need a new virtualenv, so let's do phyton dash 3:03 mvenv and let's call this one flask basics two, 3:08 this is my second try and it builds the virtualenv for 3:13 me and we have our requirements.txt file. 3:17 And then we're gonna do pip install -r 3:22 .\requirements.txt and it'll install everything. 3:26 Now, I did have a couple of issues with line endings. 3:31 If you have issues with line endings to where it's like what is it? 3:34 Embedded null character. 3:39 It could be that your line endings are something that 3:41 pip isn't currently understanding. 3:43 Just make sure the file is saved just like UTF-8. 3:45 And the line endings are Windows or 3:48 Linux should both work, but swap it back and forth if we need to. 3:52 Your text editor should be able to do that unless your text editor is idle. 3:55 In which case just install each package on it's own. 4:00 Anyway though, you should be all set up and ready to go for 4:03 working with Python locally. 4:06
You need to sign up for Treehouse in order to download course files.Sign up