Python Modules and how to Import Python Modules?

In this Python article we will learn about import keyword and how to use it.

1. What are Modules in Python?

A file with python code in it is known as a module, it can consist of either function, class, variables, or any other object details or the runnable code.

If we write a python code in Python interpreter and exit the interpreter then all the code written in the interpreter is lost and we cannot reuse it. So if we want to reuse the code then it is better that we use some text editor or IDE to write that code, save it as a .py file and run that file in the interpreter. This process is known as scripting.

Now we can reuse the definitions from this file again and again in different programs without the need to rewrite or copy the functions or other definitions.

Example : Let’s create a file name

def speak():
    print("It's a beautiful day to save lives")

def speaker():
    print("Derek Shepherd")

Remember: The file name without the extension (.py), is the name of the module. In the example above dialogue will be our module.

But what to do with this module or how to use it in any other program code? For that, we have an import keyword in python.

2. How to import Modules in Python?

Using the import keyword we can use any python source file as a module in any other python source file.

Whenever an interpreter comes across an import statement it searches in the list of directories where it searches for the module to be imported.

Syntax : import module

Let’s see how to import the module dialogue, which we created in the above example.

import dialogue

It's a beautiful day to save lives
Derek Shepherd

3. from… import…

Using from statement we import only the specific functions, variables, etc from the module. By using this format we can skip importing the whole module.

Syntax : from module_name import function

Let’s see how we can use from statement to import only the speak function from our Dialogue module.

from dialogue import speak

It's a beautiful day to save lives

And now if we try to use the speaker function then it will throw an error.

from dialogue import speak

File "c:\Users\parih\Desktop\Kaggle\", line 3, in 
NameError: name 'dialogue' is not defined

Remember: If we want to import all the functions from a module then we can use import *

Syntax : from module_name import *

Example : from dialogue import speak

4. dir() – Built in function

dir() is used to list the functions or elements that a module contains.

Example: If we check our module dialogue then the output will be some default functions along with our custom defined functions.

import dialogue
print( dir(dialogue) )

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'speak', 'speakere']

5. Creating a namespace in Python

Sometimes we feel that the name of the module is really lengthy or too time-consuming to write so to make it short we create its namespace using the keyword as.

Having a lengthy name is not the only reason to use the namespaces, namespace can be used even when we are having conflicting names from different modules or we want to assign better descriptive names to a module so that our code is more readable and clean.

Syntax : import module_name as namespace_name

import dialogue as dia

It's a beautiful day to save lives
Derek Shepherd

6. Conclusion

In this python article we learned about:

  • What is a Python module?
  • How to use the import statement
  • How to use from.. import.. statement
  • Use of built in function dir() to list Python module details
  • Creating a namespace of a module

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x