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


Talk Python's Mastodon Michael Kennedy's Mastodon