Python for the .NET developer Transcripts
Chapter: Package management and external libraries
Lecture: Installing the Python packages

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