Practical Guide to Python

External Libraries & requests Library

Nina Zakharenko

Nina Zakharenko

Microsoft
Practical Guide to Python

Check out a free preview of the full Practical Guide to Python course

The "External Libraries & requests Library" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina recommends copying the "pip install" command from the Python Package Index website to ensure the correct external package is installed. The requests library loads data from an external API. The status_code property in the response object can be used to handle successful or erroneous requests.

Preview
Close

Transcript from the "External Libraries & requests Library" Lesson

[00:00:00]
>> Now, to install external packages you use pip, but make sure that you know your packages there's a lot of packages in the python package index. You can browse them at pypi.org. So for example, let's look up a request. And we should see, The library that we're looking for right here.

[00:00:30]
Make sure that you check and see, well, okay, this is 43,000 stars on GitHub it's probably the right thing. There are a lot of libraries that have similar names and there have been malicious libraries that have popped up for common misspellings. So it's very important to verify your external package names before you run the command.

[00:00:53]
Better yet, copy the package name straight from pypi. If we go here, there's just a big blue button here where you can copy it. So you don't have to risk typing it in and getting it wrong. A little bit about the requests library. We saw it early on in the course, it allows us to make requests and get back responses.

[00:01:17]
The first thing we would want to do is import it, and let's use the shibe online API which returns, Shibe dog. Is that the right way of pronouncing it, shibe? I'm gonna go with that but tldr API with cute puppies. So now that you've installed requests in your virtual environment, you can copy this file.

[00:01:55]
And we're going to call it shebi.py. And we're going to run it. And what we did here was define a URL. We got a response by calling request.get. We found out what the status code was, in this case it was 200. We got the results as JSON and then we printed it out.

[00:02:25]
So we got a list with an image URL. I'm going to click on it. It is adorable. No, puppy. Cool, so how do we deal with errors? If we try to pass in a non existent URL, we're going to get a 404. So, we want to make sure that we check the status code.

[00:02:52]
And we can also pass in different parameters when we use the requests library just like this. And let's copy this whole thing over. Oops, that is not what I wanted. No, I have angered my rebel. Let's reopen that again. So here, I am now defining a parameters count 10 and I'm passing in those parameters with the request.

[00:03:33]
I'm getting that JSON. And now because it's a list that has multiple items in it, I am looping over it and printing it out. Now I have 10 puppy pictures to choose from. I'm gonna open this one. Very cute. So that's how you pass in parameters. That's the 1000 level overview of the request library.

[00:03:59]
If you want to learn more about it after the class, take a look at the Quickstart. It's very powerful. So we are now going to take a short break. Let's say about 20 minutes and over break take a look at the practice for object oriented Python. Go ahead and skip over the practice for inheritance and come back to it after class, it's a more involved topic and follow through with the practice for libraries and modules.

[00:04:28]
Once we come back from break, we're gonna talk about our final topic, which is web frameworks and Django. I'm sure your brains are totally full of knowledge right now, before we go on break, are there any questions? Can you have multiple tries? Generally not. No, I think that's a syntax error in python.

[00:04:54]
And for questions like that, I would recommend that you type them out in the rubble, that would be a very quick and easy way to get an answer for that. Yeah, there we go. So as soon as I tried that I get a syntax error. Each try needs to have an accept associated with it, one or more.

[00:05:17]
And then you can have separate try blocks later on in your code.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now