Create and Import modules in Python - GeeksforGeeks

29 Dec, 2019
In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and packages. A package is a collection of modules in directories that give structure and hierarchy to the modules.
Reusability: Working with modules makes the code reusability a reality.
Simplicity: Module focuses on a small proportion of the problem, rather than focusing on the entire problem.
Scoping: A separate namespace is defined by a module that helps to avoid collisions between identifiers.
A module is simply a Python file with a .py extension that can be imported inside another Python program. The name of the Python file becomes the module name. The module contains definitions and implementation of classes, variables, and functions that can be used inside another program.
Example: Let’s create a simple module named GFG.
''' GFG.py '''  # Python program to create# a module    # Defining a functiondef Geeks():    print("GeeksforGeeks")  # Defining a variablelocation = "Noida"
The above example shows the creation of a simple module named GFG as the name of the above Python file is GFG.py. When this code is executed it does nothing because the function created is not invoked.
To use the above created module, create a new Python file in the same directory and import GFG module using the import statement.
Example:
# Python program to demonstrate# modules    import GFG  # Use the function createdGFG.Geeks()  # Print the variable declaredprint(GFG.location)
Output:
GeeksforGeeks
Noida

The above example shows a simple module with only functions and variables. Now let’s create a little bit complex module with classes, functions, and variables. Below is the implementation.
Example: Open the above created GFG module and make the following changes.
''' GFG.py '''  # Python program to demonstrate # modules    # Defining a functiondef Geeks():    print("GeeksforGeeks")  # Defining a variablelocation = "Noida"  # Defining a classclass Employee():          def __init__(self, name, position):        self. name = name        self.position = position              def show(self):        print("Employee name:", self.name)        print("Employee position:", self.position)
In this example, a class named employee has been declared with a method show() to print the details of the employee. Now open the Python script for importing and using this module.
Example:
# Python program to demonstrate# modules      import GFG     # Use the class createdemp = GFG.Employee("Nikhil", "Developer")emp.show()
Output:
Employee name: Nikhil
Employee position: Developer

All the object from a module can be imported as a variable. This prevents the usage of the module name as a prefix.
Syntax:
from module_name_ import *

Example: We will use the above created GFG module.
# Python program to demonstrate# modules      from GFG import *     # Calling the functionGeeks()  # Printing the variableprint(location)  # Calling classemp = Employee("Nikhil", "Developer")emp.show()
Output:
GeeksforGeeks
Noida
Employee name: Nikhil
Employee position: Developer

A module can be imported with another name, specified by the user.
Example:
# Python program to demonstrate# modules      import GFG as g     # Calling the functiong.Geeks()  # Printing the variableprint(g.location)  # Calling classemp = g.Employee("Nikhil", "Developer")emp.show()
Output:
GeeksforGeeks
Noida
Employee name: Nikhil
Employee position: Developer

python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists