Introduction
Visual studio code primer
Modules and packages
Virtual environments and package management
Intermediate Python Concepts
Built-in libraries for daily use

The import statement

You can import a Python module by using Python’s import keyword. Module imports are often placed at the top of a script but don’t have to be. E.g., sometimes you want to dynamically decide if you need to import a module, especially when that module takes up a lot of memory or other resources.

A simple Python module

Let’s create a simple module, called mymodule.py. All it contains is the following function:

def my_function():
    print('Hello world!')

We can import this module in another script, but you need to make sure the two files are in the same directory. To import the module, use the following command:

import mymodule

This imports the entire module and makes it available to the rest of the program under the name mymodule. After importing the module with import, we can use the function by using the following command:

mymodule.my_function()

To see this in action, you can run and play around with the following code crumb:

Python import and use a module

Importing specific parts of a Python module

We can also import specific parts of a module, like a function, variable, or class. To do so, use the following syntax:

from mymodule import my_function

The my_funcion function is now available to the rest of the program under the name my_function, as if it was defined inside the file itself. So we can use it like this:

my_function()

In the same fashion, we can import multiple elements from a Python module. To do so, use the following syntax:

from mymodule import my_function, my_variable

It’s up to you when to do what. Sometimes, you need a function so often that referencing the entire module each time looks messy. Other times, it’s better to import the module name because you need a lot of different functions from that same module. Another consideration might be that it’s more clear to the readers of your code where a specific function, variable, or class is from when you reference it by using the entire module name.

Wildcard imports (are bad practice)

When a module has a lot of functions and other elements that you want to import, it can be tempting to import them all at once with a special from module import * syntax. To import everything from a module, we can use the following syntax:

from mymodule import *

You should avoid using the * syntax in most cases, because it can lead to unexpected results, especially when using third-party modules over which you have no control. After all, you don’t know what’s inside a third-party module, or what will be inside of it in the future. By importing everything, you ‘pollute’ your namespace with unnecessary variables, classes, and functions. You might even overwrite some existing definitions without noticing. My advice: only import specific elements to stay in control of your namespace.

Import aliases

Sometimes Python module names are not what you like them to be. Perhaps you need to type the name a lot, like in a Jupyter Notebook. In this case, you can use an alias to make the module name shorter. For example, if you have a module named mymodule.py, you can import it as m:

import mymodule as m

Some common aliases that you will encounter, especially in fields like data science, are:

  • import numpy as np
  • import pandas as pd
  • import matplotlib.pyplot as plt
  • import seaborn as sns