Python for the .NET Developer Transcripts
Chapter: Package management and external libraries
Lecture: Installing the Python packages
0:00 As we saw, Python has PyPI with over 200,000 packages. You would think we should be able to recreate that application you saw over in C#
0:10 that screen-scraping one that just downloads the header the h1 and then shows it to us. Turns out, of course we can.
0:17 We're going to use three cool libraries to do it. So let's talk about a couple of things here. First of all, in order to declare it
0:23 these are the requirements that my program depends upon we typically have a requirements.txt or there's a couple of other formats
0:30 that are becoming popular but requirements.txt is probably still the most popular. And the idea is you just list the names of the packages
0:38 like if I wanted to use HTMLAgilityPack like as you saw, I would just write that boom, it's ready. Notice that PyCharm's are like we got to install it
0:46 you want me to do that? It actually doesn't exist for Python I don't think. We need something called Beautiful Soup instead.
0:53 But you just list the names of them here. Sometimes you would put the version like 0.1.0. like that actually double equals
1:01 and then it says oh you have the wrong version of Colorama and you want me go get that? Not right now. So you can put them there.
1:09 A lot of times what people do is, they'll just play around and just install them manually and then go back and recreate
1:15 this requirements.txt, once they have decided on what they actually need for their program. So let me show you how you would ad hoc
1:22 install it not just play with this requirements.txt purely within PyCharm. So lets go over here. Now first thing to notice
1:36 if I want to work with it, the tool we work with work is called pip. And I can thing do like pip list, but the problem is
1:44 if I ask which pip, notice it wants itself to be updated but kind of irrelevant we don't care about it okay. Which, hold on, I kind of over wrote it.
1:54 Which pip3. Here we go, got it to save finally. So it's out of my primary Python 3 right now 3.7 on the system.
2:04 This is not the same one if I say pip list as you saw it there is tons of stuff here but the only thing we declared is Colorama what's going on?
2:12 Or remember you have to always if you're using a virtual environment then you almost always should you need to make sure that you activate them.
2:19 On windows that would be venv\Script\Activate. Not that, not here though. We have to say dot to apply to this shell and then venv/bin/activate
2:30 so on Mac and Linux this is a command and notice we have this now. Okay, so now we can install stuff we can do a couple things like I said
2:38 pip install colorama. We did this earlier so it's already good let's make this warning go away apparently a new version just released.
2:47 So you can use pip to manage itself it's very meta, kind of satisfying that way. Seeing as we already have colorama there
2:53 and we do a pip list we should have the two tools that manage packages and then Colorama apparently that is the version we actually had.
3:01 And maybe we want some more, so if we're going to let's say we want to work with pip install bs4 for Beautiful Soup
3:10 we hit that and notice it either downloads or used the cache version and it's actually pulling down a couple of things that it needs
3:17 so it takes the transitive closure of all the dependencies of all the things you've asked for, right? So this is pretty cool, now we can do pip list
3:23 and we should have those listed there but if we have one of these requirements. Let's say, let's put bs4 and let's put one more thing in here httpx.
3:34 Would let me making a HTTP client request we could use requests, that's most popular but later we going to use a feature of
3:41 this library that's not available on requests and I'm going to take this same code and copy it. So let's just roll with this one, okay?
3:47 So if I wanted to install all the requirements of what we got here, I would say. Where are, that's it, right here. Yes, we can say pip install -R
3:57 to say here's a requirements file not the name of the thing I want but a just a file, listing of them. And I would just say this
4:06 and it's going to go through one spells install correctly. It's going to go through all the stuff in there and install them, notice it installed
4:13 a bunch of other stuff, but many things like this one bs4 with already satisfied. And the way this works is basically up here
4:22 these are just, like you can think that there's like an implicit pip install this, right? So whatever you write, whatever commands you want
4:34 to issue to pip, you just put them in here like this so you can have it by itself you can have version names and so on. Right, so that's how these
4:42 requirements.txt files work. You can even include other ones like let's suppose, we had over here. I'm not going to do it.
4:50 Or let's suppose we had another file we could have -R other.txt if that also had requirement. Just imagine, pip install whatever
5:01 each line in this text file is you can also have comments with hash. PyCharm itself, if it's at the top level
5:08 try to run it you can also open the terminal and it will automatically activate the environment and then you just pip and install -R that one
5:15 pip install -R requirements that right, but they should all be satisfied. Finally, all that's standard Python finally though, if you're in PyCharm
5:29 you can pull up the settings you can go to the project, project interpreter and here you have basically a GUI version of pip.
5:39 And I really like this, it shows that hey there's all these different things here I can get an update of that one. I'm not sure I got the old one
5:45 but probably something I installed that I prefer to have the old one, so I'm not going to mess with that, but like setup's tools I could update that.
5:53 Click that button, give it a second, there you go it was updated successfully and so on. I can even add more probably here
6:00 and I could look for Async, here's a whole bunch of stuff to do with Async, right? Like a install package, this is like the
6:06 manage NuGet packages in Visual Studio and what happens here or here or here this is like, you know installed dash package
6:16 in the NuGet package console. Once we have these installed then they're just available to our project. So let's go over here and mark that directory
6:30 of the source's route, give it a program I call it Scrape, that's what we going to do. Now if I want to work with like say httpx
6:36 I would just say, import and I hit h right there it is, right? Because it installed in that virtual environment for this project, basically the one
6:46 that's active now it shows up. And we can do our main business and over here let's just say using Python packages.
6:55 Let's just print out, just to show you that we were able to import this just the version number of httpx.
7:02 Alright, so let's try to run Scrape see what happens. Boom, using Python packages just like that. Also a quick comment
7:12 when I run stuff in Visual Studio it pops into another terminal, like a separate window. And here it runs it at bottom and for some reason
7:19 you want the separate window, you just copy what it's doing right there open this up and just paste it there you can see same basic thing right.
7:30 I find this pretty handy to have it down here you don't have to keep jumping back and forth but if you don't want to run it separately
7:35 just copy the command that PyCharm's using most of the time that works. So that's how we use these external packages.
7:43 You will identify the name of them you pip install them either by putting them in the requirements document or literally typing pip install the thing
7:51 and then they're just available. You import the name and you can start using the things. Like I could say get and you know get a URL here
7:57 and off it goes.