Python import With Examples

Python import statement is used to import modules that we want to use in our program. Python modules are python scripts containing utility functions, types, classes etc. There are many modules that we use regularly in python programs such as sys, os, collections etc.

Python import

If we want to import python built-in modules or any third party module installed using a package manager such as PIP, then we can very easily import and use them in our program.

Python looks for modules and packages into sys.path property. This path always contains the current directory from where the script is executed, so any module in the current directory can be imported as is.

Python imports are case sensitive, so import sys and import Sys are looking for different modules to import.

Python looks for a module into the built-in modules first. If not found, then it searches for modules in the current directory. So if we have math.py file in the same directory as our main script, then it will be loaded when import math is called if ‘math’ module is not in the built-in modules. You can get a list of built-in modules using sys.builtin_module_names. Below image shows the built-in modules in my python installation.

Python biltin module

Python import class/functions from module

We can import specific classes from module too. This way we can import specific parts of a module and use it. This also helps in writing fluent code. We can achieve this using from keyword with the import statement.

Python import user-defined module

When we create a python script, we can import it into another python script using its name. Let’s say we have following directory structure with multiple python scripts.

Python import

We have following functions defined in utils.py file.

We can import it into python_import_examples.py and use its functions.

Output:

Python import as

We can define our own name for the imported module using import as statement.

The result will be the same as the earlier program.

Python import from another directory

If the python script we are importing is in the same directory, then we can import is easily just like built-in modules. However, if the python script is present in another directory, then we can use importlib library to import them as a module.

Let’s say our strutils.py has following functions:

Now let’s see how to use importlib to import this python script into our example.

Python import class from another file

We can import scripts and use the classes defined in them using importlib too. Let’s say we have classes Person and Student defined in myclasses.py file.

Here is the example code where I am using the earlier defined function to import this script and use its classes.

Notice that we can keep any name for the module we are importing, it’s similar to using import as statement.

There is another way to import scripts from another directory using the sys module.

This is useful when we want to import only specific classes and functions from the imported file. Also, using this code is much easier to understand.

Summary

Python import statement allows us to import modules, python scripts, specific classes and functions from modules. It’s very easy to use and since most of the times we work on built-in modules or modules installed using PIP, we don’t need to write logic to load scripts from another directory.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: