Practical Guide to Python

Libraries, Modules & Imports

Practical Guide to Python

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

The "Libraries, Modules & Imports" 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 explains how the import keyword is used to import either an entire module or just a function from a module. Python's standard library contains a number of useful modules for handling dates, system-specific functions, and json data.


Transcript from the "Libraries, Modules & Imports" Lesson

>> When you're working with libraries in Python, you're going to make use of the import keyword. So let's take a look at how this looks like. You can import packages that are in the standard library without needing to do anything special. So for example, random is part of the standard library.

And this random module, if I wanted to see what was in it, I could call dir on it. Has a method here called randint. If I wanted to know what it does, I could pass that into help. So returns a random integer in the range of a and b including both end points.

So I can call random.rantint between 0 and 100, and that will give me a random number each time I run it. You can also specify that instead of importing everything that it is in this random module, you can say from the random module import, and then the specific name of the function that you want to use.

And then I can call randint this way without any extra qualifications. You'll get more comfortable with this concept as you advance in your python journey. But know that, if you are going to be using a lot of or all of the definitions from a module, go ahead and import that top level module.

If you just want to use one thing, stick with the best practice of only importing what you need. You can also install libraries with pip. And generally you want to run pip this way python-m pip. Remember the -m flag means from the next argument as a standalone program.

And this makes sure that we're always using the version of pip that is associated with the version of Python in our virtual environment. Because unfortunately, you can also end up with multiple versions of pip installed. Sometimes you might have a pip three for Python three. So this is the guaranteed way of getting the right package installed.

Go ahead and do a python-m pip install requests in your virtual environment. And we're going to use that external library in the last practice for this chapter. Make sure that you are in your terminal and your virtual environment is activated and do python -m pip install requests. And you should see an install here, I've already installed it.

So it says requirement already satisfied. Now a little bit more about modules and imports. Python has this batteries included philosophy. There is a rich and versatile standard library available without having to download separate packages. There's a lot of useful stuff in here like working with JSON we saw earlier working with times and dates.

If you want to make your own modules, just make a new folder and put your code in there. And that way you can import everything that is in that module just like this. In older versions of Python, you had to have an empty file in folders for Python to recognize them as modules.

In Python three, that is no longer the case. And when you have time, you can go through here and look at best practices. We're not going to talk about tests right now, but I will cover tests when we talk about Django. Know that you should write them. They are a cornerstone of solid Python programs.

And the unit testing library is built in to Python. You just have to import unit test and then you have a lot of features available. In the course website, you'll also see that there is a cheat sheet for all of the test case assertions that you might commonly use when you are unit testing your code.

Like assert equal, assert, not equal assert true assert false. You'll be using a combination of these in your unit tests.

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