Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Modules and Packages
Lecture: State your requirements

Login or purchase this course to watch this video and the rest of the course contents.
0:01 One of the challenges of deploying your code, your set of Python scripts,
0:04 to run on other systems is to communicate exactly what you depend upon,
0:09 so that you make sure that that system has the right things installed.
0:13 We saw that virtual environments allow you to control this,
0:16 but how do you state it, how do you help someone grabbing your library
0:20 or your app know what they've got to install?
0:22 Let's look at that.
0:24 Over here I have a little app it's going to do some downloading
0:27 and it's using 3 packages,
0:29 now these would be interspersed throughout your app of course, right,
0:31 but in this case, we just have them listed here.
0:35 So we are using request to download some the homepage in Google,
0:38 show the status code, imagine somewhere else using records for sql,
0:42 over here we are using user accounts and correct hashing with pass lib and so on.
0:47 So if I run this in PyCharm, on my system, it works great,
0:50 I've got 200 back from Google.
0:52 Let's imagine I was going to take the same code and run it somewhere else;
0:56 to simulate running it on another machine,
1:00 let me go to that virtual environment we created before and I'll just activate it,
1:02 so I'll say .user/screencaster/python_environments
1:05 this is what I created I'll say activate, ok the prompt changes,
1:08 now if I say pip list you'll see I don't have all the things we need,
1:13 I do have requests but I don't have records or pass lib,
1:17 let me see what happens if I try to run that program.
1:21 So we are going to run it and oh, that didn't go so well,
1:25 I guess we needed records, we would go pip install records and we tried again,
1:29 we'd see pass lib and that's because- and that would be actually the easy case
1:33 because it's all in one file, but normally,
1:36 it would be spread out so eventually you would get to some action
1:39 that import a module that oh, well that crashes too
1:41 because there is some other missing piece.
1:44 So how do we solve this?
1:46 It's quite simple, we can come over here and I would put whatever my code is,
1:49 I want to run, I would give it a requirements.txt. and in here,
1:55 the format is super simple, you just list the names,
1:58 one per line, of the packages you depend upon,
2:03 so if I copy this and I come back over to my
2:06 "other system" where I have, remember,
2:10 I am using this pip from here,
2:13 if I say pip install-r for requirements file and I give it that,
2:19 it's going to look at all the requirements we've specified download them,
2:26 and make sure the system is ready to roll.
2:29 As long as I have kept that requirements file,
2:31 up to date with what my app actually uses, we're good.
2:35 And PyCharm if we are working in certain types of environments
2:38 actually is really good about managing that for us,
2:41 if it sees you work inside of a package like say a pyramid web app,
2:44 and it sees you using some external package,
2:46 that is not listed in your setup install requirements
2:49 it'll actually automatically put them there for you.
2:51 Ok, so now if we say pip list, this looks like a much better chance,
2:56 let's try running our little app again, we should just see 200
2:59 as it talks to Google and gets good response code, server says, 200.
3:05 So that's the requirement.txt file add all of your requirements to it,
3:09 just one package name per line and then pip install -r requirements.txt.
3:14 In a graphic, our app is using some external modules, or external packages,
3:19 here we are just showing requests, in order for this script to run at all,
3:23 we are going to need a request installed on the system that's going to be running it.
3:27 How do we communicate that?
3:29 Well, we have our requirements.txt that lists out our various requirements,
3:31 then we pip install -r, give it the requirements text file and boom,
3:36 problem solved.