Create and Import modules in Python

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.

Advantages of 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.

Creating and Importing a module

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

''' GFG.py '''
  
# Python program to create
# a module
  
  
# Defining a function
def Geeks():
    print("GeeksforGeeks")
  
# Defining a variable
location = "Noida"

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# modules
  
  
import GFG
  
# Use the function created
GFG.Geeks()
  
# Print the variable declared
print(GFG.location)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

''' GFG.py '''
  
# Python program to demonstrate 
# modules
  
  
# Defining a function
def Geeks():
    print("GeeksforGeeks")
  
# Defining a variable
location = "Noida"
  
# Defining a class
class 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)

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# modules
   
   
import GFG
   
  
# Use the class created
emp = GFG.Employee("Nikhil", "Developer")
emp.show()

chevron_right


Output:

Employee name: Nikhil
Employee position: Developer

Import all names

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# modules
   
   
from GFG import *
   
  
# Calling the function
Geeks()
  
# Printing the variable
print(location)
  
# Calling class
emp = Employee("Nikhil", "Developer")
emp.show()

chevron_right


Output:

GeeksforGeeks
Noida
Employee name: Nikhil
Employee position: Developer

Import with renaming

A module can be imported with another name, specified by the user.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# modules
   
   
import GFG as g
   
  
# Calling the function
g.Geeks()
  
# Printing the variable
print(g.location)
  
# Calling class
emp = g.Employee("Nikhil", "Developer")
emp.show()

chevron_right


Output:

GeeksforGeeks
Noida
Employee name: Nikhil
Employee position: Developer



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.