Check out a free preview of the full Practical Guide to Python course:
The "Libraries & Modules Practice" 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 walks the students through the Libraries and Modules practice.

Get Unlimited Access Now

Transcript from the "Libraries & Modules Practice" Lesson

[00:00:00]
>> We created a basic programme that we can run on the command line. We are using a main function here. And we also talked about working with files. So we worked with a cities dot JSON file. In our example here, we need to input JSON from the standard library, and then we can go ahead and work with this file.

[00:00:29] Now, if we wanted to loop over each city in this list and also provide the index we can use the enumerate function to do that. And remember that using the open keyword on its own is frowned upon because it doesn't do any of that resource cleanup that you need to do when you're working with files.

[00:00:56] So make sure that when you are working with files, you use a context manager. So here when I say with open, everything that is indented under that with statement will get sandwiched in between the code that opens the file and then automatically closes it and handles any exceptions without you having to worry or think about it.

[00:01:22] When we handle exceptions, we use a try and accept block. Here we are accepting on a JSON decode error, the most specific error possible for this particular exercise. And then we can print out a specific error message as well as telling the user Hey, sorry. There was an error decoding that JSON file.

[00:01:44] We talked a little bit about using the requests library, about getting a response, parsing that response that comes back and response .JSON. Before you use the request library, you need to make sure that you go ahead and install it in your virtual end with PIP Otherwise, it's an external package.

[00:02:06] It's not part of the standard library. So when you try to import it if you don't have it installed, you're going to see an error. There is some additional practice here for working with the request library that I recommend you go through on your own time. Remember that a Python has a huge standard library with a huge number of packages.

[00:02:30] If you wanna do something, there's probably a package included. You can use some of the more common ones in this practice. Here we create a new file and we use the OS module which stands for operating system. To get the current working directory sis is another common Lee used library.

[00:02:52] You can use sis to read command line arguments if you're trying to run your Python program as a script and pass in variables on the command line. Remember that pypi is the Python package index and it's a service that helps you find third party Python libraries and packages.

[00:03:11] But be mindful to type in the right thing not have any typos because there are instances where there have been malicious packages that have lived at mistyped names, you can search pypi from the website, you can also use it on the command line as well. And to install packages just do Python dash m pip install requests.

[00:03:37] That command is a little bit more verbose than just doing pip install Which you might see in other examples and tutorials. But this guarantee is that you are using the right version of PIP for your version Python and I promise you it will save you lots and lots of pain.

[00:03:55] I'm not going to cover adding tests at the moment but tests in Python are extremely important. So, if you go back to any chapters, I recommend that you visit this one.