Python import:

Python provides a few different ways to import modules and packages. In this tutorial, we’ll take a look at how importing works and the various ways we can import definitions from a module.

The Standard Python import Statement:

When we see how to create a module, we also learned how to import the module as a singular entity into other Python files. To reiterate this, we use the following format to import an entire module under its namespace.

Terminal
import my_module_name

By doing this, we’re able to access anything exposed by the module by chaining off of the module’s name.

Occasionally, we might have a naming conflict when importing a module. In those cases, we can also use the keyword as in the import statement to change the identifier that we use to represent the module.

Let’s create a python module and write a few functions that can manipulate strings.

~/using_modules/helpers.py
def extract_upper(phrase):
    return list(filter(str.isupper, phrase))

def extract_lower(phrase):
    return list(filter(str.islower, phrase))

Now let’s import the module inusing_modules/main.py so that the helpers module is accessed using the h name.

Terminal
import helpers as h

name = "Chandra Shekhar"
print(f"Lowercase letters: {h.extract_lower(name)}")
print(f"Uppercase letters: {h.extract_upper(name)}")

The name of h isn’t great, but it does demonstrate that we can change the name of modules when we import them. If we run this script, we will see there’s no difference in the output.

Terminal
$ python main.py
Lowercase letters: ['h', 'a', 'n', 'd', 'r', 'a', 'h', 'e', 'k', 'h', 'a', 'r']
Uppercase letters: ['C', 'S']

Python Module importing from:

More often than not, we don’t need to use everything provided by a module. In these cases, we can leverage the from the version of an import statement to import only the definitions we need from the module, and then we can access them directly.

To demonstrate how to do this for multiple functions, let’s directly import the functions from our helpers module. The from statement works like this:

Terminal
from <MODULE_NAME> import <definition>, <definition>, <etc.>

Here’s what it looks like in main.py:

~/using_modules/main.py
from helpers import extract_lower, extract_upper

name = "Chandra Shekhar"
print(f"Lowercase letters: {extract_lower(name)}")
print(f"Uppercase letters: {extract_upper(name)}")

It’s worth noting that now we don’t have access to the helpers name in our code at all. If we change our extract_upper line to be chained off of helpers name it will cause an error.

Lastly, we can also combine the as keyword with each definition that we’re importing to explicitly rename that definition.

~/using_modules/main.py
from helpers import extract_lower as e_low, extract_upper

name = "Chandra Shekhar"
print(f"Lowercase letters: {e_low(name)}")
print(f"Uppercase letters: {extract_upper(name)}")

Importing Everything from a Module:

The final way we can import definitions from a module is to import all of them at once by using *. This is generally not the recommended way of importing things, but sometimes a module provides a lot of functions that we’ll be using, and we don’t want to explicitly import them one at a time.

Let’s utilize the * to import our two functions from the helpers module without explicitly naming them.

~/using_modules/main.py
from helpers import *

name = "Chandra Shekhar"
print(f"Lowercase letters: {extract_lower(name)}")
print(f"Uppercase letters: {extract_upper(name)}")

Once again, if we run this, it will work just as it did before.

Terminal
$ python main.py
Lowercase letters: ['h', 'a', 'n', 'd', 'r', 'a', 'h', 'e', 'k', 'h', 'a', 'r']
Uppercase letters: ['C', 'S']

References:

Happy Learning 🙂

Leave A Comment