DEV Community 👩‍💻👨‍💻

Cover image for Importing Modules In Python
Emmanuel Tanimowo
Emmanuel Tanimowo

Posted on • Originally published at hashnode.com

Importing Modules In Python

What is a Module?

A Module is a Python source code that contains functions and/or classes you want to make use of.
For instance, the math module contains various mathematical functions. To use these functions in the math module, you must import them. An import statement tells Python to make the functions/classes in the module available in your current program file.

There are several ways to import a module in Python:

- Importing an Entire Module:

To import an entire module let's say math module, you make use of the import keyword, followed by the function name.

Syntax: import function_name

To import the math module; import math, this statement tells the Python interpreter to load the contents of the module into memory, that is making the functions and/or classes stored in the math module available in your current program file. After importing a module, you have can make use of all the functions or classes it contains.

To call each function in the module, you make use of the dot notation; the module's name followed by a dot then the name of the function.

Syntax: module_name.function_name

Let's use the sqrt function in the module as an example, when it is called the square root of the number entered is returned:

import math
n = math.sqrt(25)   # get the square root of 25
print (n)
>>> 5.0
Enter fullscreen mode Exit fullscreen mode

- Importing a specific Function or Class:

There are times you only want to make use of a specific function or class from a module. In such a case, you use the from keyword with the import statement.

Syntax:

from module_name import function_name

This statement causes only the function to be imported from the module.

Here's an example of how to import and use only the sqrt function from the math module:

from math import sqrt
n = sqrt(25)   # get the square root of 25
print (n)
 >>> 5.0
Enter fullscreen mode Exit fullscreen mode

You can as well import several specific functions or classes from a module. To do that, specify the names of the functions or classes to be imported separated by commas:

Syntax: from module_name import function_a, function_b, function_c

An example showing how to import the sqrt function and radians function from the math module:

from math import sqrt, radians
n = sqrt (25)   # get the square root of 25
c = radians (180)   # convert degree value into radians
print (n)
>>> 5.0
print (c)
>>>> 3.141592653589793
Enter fullscreen mode Exit fullscreen mode

- Using the as keyword to give Alias:

If the name of the module you want to import is too long, you could give it an alias- an alternate name to use when using the module (like a nickname). When you give a module an alias, it renames the module's name using the provided alias.

An example of how to import the math module using an alias:

import math as mth   # given math module an alias `mth`
n = mth.sqrt(25)   # get the square root of 25
print (n)
>>> 5.0
Enter fullscreen mode Exit fullscreen mode

In addition, if the name of a function or class you want to import conflicts with an existing name in your current program file, or the name of the function or class is too long, you could also give it an alias.

An example of how to import only the radians function from the math module using an alias:

from math import radians as rad   # given the function radians an alias `rad`
c = rad(180)
>>> 3.141592653589793
Enter fullscreen mode Exit fullscreen mode

- Wildcard imports:

You can tell python to import the entire contents of a module by making use of the asterisk operator(*).

Syntax: from math import *

Having imported all the contents in the module into your current program file, each function can be called by its name:

from math import *
n = sqrt(25)   # get the square root of 25
print (n)
>>> 5.0
Enter fullscreen mode Exit fullscreen mode

Nonetheless, it's best not to use this approach, because it can lead to name clashes while importing several modules. A name clash could occur- when two or more modules contain identifiers with the same name and are imported into the same file.
To avoid name clashes, you should import the specific functions or classes you need from the module, or you import the entire module and make use of the dot notation.

Thanks for taking your time to read, please feel free to drop some comments below.

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠