Create and Import modules in Python
Last Updated :
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.
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.
Python3 1==
''' GFG.py '''
# Python program to create
# a module
# Defining a function
def Geeks():
print("GeeksforGeeks")
# Defining a variable
location = "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:
Python3 1==
# Python program to demonstrate
# modules
import GFG
# Use the function created
GFG.Geeks()
# Print the variable declared
print(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.
Python3 1==
''' 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)
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:
Python3 1==
# Python program to demonstrate
# modules
import GFG
# Use the class created
emp = GFG.Employee("Nikhil", "Developer")
emp.show()
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.
Python3 1==
# 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()
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:
Python3 1==
# 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()
Output:
GeeksforGeeks
Noida
Employee name: Nikhil
Employee position: Developer
Similar Reads
How to create modules in Python 3 ?
Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t
4 min read
Import module in Python
In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
Import Modules From Another Folder in Python
In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder. It can be done in two
2 min read
External Modules in Python
Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
Python - Import module outside directory
Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module into that file. If the module exists in the same directory as the file, we can directly import it using the syntax import
4 min read
Cmdparse module in Python
The Class which provides a simple framework for writing line-oriented command interpreters is called cmd class. These are often useful for administrative tools, prototypes and test harnesses that will later be wrapped in a more sophisticated interface. The command-line interface can be easily made u
6 min read
Python datetime module
In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python
14 min read
Built-in Modules in Python
Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
Importerror: Cannot Import Name In Python
One common error that developers encounter is the "ImportError: Cannot Import Name." This error occurs when the Python interpreter is unable to import a particular name or module within your code. In this article, we'll explore what causes this error and provide solutions to fix it. What is ImportEr
4 min read
C API from Extension Module in Python | Set 2
Prerequisite: C API from Extension Module in Python | Set 1 Let's see an example of a new extension module that loads and uses these API functions that we build up in the previous article. Code #1 : C #include "pythonsample.h" /* An extension function that uses the exported API */ static P
2 min read