How to Import Modules in a Python Script

In Python, to use functions from an external module, you must first import the module into your program. This is done with the import statement, followed by the module's name.

How to Import a Module

import module_name

If the import fails, it means the module isn’t installed in your Python environment. In this case, you’ll need to install it first.

How to Call a Function from a Module

Once a module is imported, you can call its functions by writing the module's name followed by a dot and the function name.

module_name.function_name()

Depending on the function, you might need to pass input parameters within the parentheses.

A Practical Example

In the first line of the code, I import the math module into the program.

  1. import math
  2. x = 1
  3. y = math.sin(x)
  4. z = math.cos(x)

In lines three and four, I use the sin() and cos() functions from the math module to calculate the sine and cosine of x.

To use these functions, the module name must precede the function name, separated by a dot.

The Module Name Is Mandatory

If you omit the module name and write only sin(x) or cos(x), the program will throw an error.

  1. import math
  2. x = 1
  3. y = sin(x) ERROR
  4. z = cos(x) ERROR

How to Skip the Module Name in Function Calls

Python provides a way to shorten or eliminate the need to specify the module name when calling a function.

Here’s how to do it.

Using the as Clause to Rename Modules

In Python, you can import a module and assign it a shorter alias name for easier reference.

import module_name as alias_name

This allows you to call the module's functions using the alias instead of the full module name.

Choosing an Alias: Aliases are typically short, memorable abbreviations. For instance, mt for math, np for numpy, etc. There are no strict rules—you can choose any alias you prefer.

A Practical Example

In this code, I import the math module and assign it the alias mt.

  1. import math as mt
  2. x = 1
  3. y = mt.sin(x)
  4. z = mt.cos(x)

In lines three and four, I use the sin() and cos() functions, but now I prefix them with mt instead of math.

This makes the function calls shorter and more convenient.

Calling Functions Without the Module Name

If you want to call functions without specifying the module name, you can use the from ... import statement.

from module_name import *

The asterisk (*) imports all the functions from the module into memory.

This lets you call functions directly without using the module name.

A Practical Example

Here, I import all the functions from the math module.

  1. from math import *
  2. x = 1
  3. y = sin(x)
  4. z = cos(x)

In lines three and four, I call the sin() and cos() functions directly, without referencing the math module.

This approach works without errors.

Importing a Single Function

In Python, you can choose to import only a specific function from a module instead of the entire library.

from module_name import function_name

This makes your script more efficient.

Why Import Just One Function?: If you only need a single function, importing the entire module is unnecessary and inefficient. Importing just the function saves memory and speeds up execution, improving computational efficiency.

A Practical Example

Here, I import only the sin() function from the math module.

  1. from math import sin
  2. x = 1
  3. y = sin(x)
  4. print(y)

In line three, I use the sin() function. Other functions in the math module are not loaded into memory.

The output of the script is:

0.8414709848078965

This saves memory and processing time.

Note: In this case, only sin(x) is available. If you try to use another function, like cos(x), it will result in an error.
an example of from import

 

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Python Modules