How to create modules in Python 3 ? - GeeksforGeeks

24 Jun, 2021
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 the pip installer, We can also create our own python module.
In this article, we will see how to create modules in python. 
Any python code consisting of functions, classes, variables can be termed as a module. We will be writing a simple module that will be having a function, a class, and a variable.
Python
# defining a class Ageclass Age:                            # defining the __init__ method    def __init__(self, name, years):            self.years = years        self.name = name              # defining the getAge method    def getAge(self):                            print("The age of " + self.name +" is "+self.years)          # defining the function hello       def hello():                  print('hello geeks!')    # creating a string s s = 'I love python!'            
Here in the above module, we have defined a class ‘Age’ having a __init__() and getAge() method which will be used to initialize the class object and getting the age of the object that would be passed, We have also defined a function hello() which will print ‘hello geeks!’ and also a string s which is ‘I love python!’.
We will name the above module made as mod.py and save it.
Now we will discuss how to import a module and use its various functions, classes, variables from other python files. For this, we will make a file main.py in the same directory as mod.py. 
We will import the module with an import statement and use the functions, classes, variable as following:-
For using the function from a module we will first import it and then will call the function with module_name.function_name(). For example, if we want to call the function hello() of mod.py in main.py we will first import mod.py and then call it with mod.hello().
Python
# importing the module mod.pyimport mod           # calling the function hello() from mod.pymod.hello() 
Output:
hello geeks!
Note: Please make sure that the main.py and mod.py are in the same directory.
We can also import only a separate function from a module to use it by writing: from module_name import function_name
And then call it directly. For example, if we want to call the function hello() of mod.py in main.py using the above method we will write the following code in main.py.
Python
# importing only the function hello from the module mod.pyfrom mod import hello           # calling the function hello() hello()     
Output:
hello geeks!
We can use the classes to make various objects by module_name.class_name(). For example, if we want to create an object in main.py by the name ‘jack’ of class Age present in mod.py. We will write the following code in main.py:
Python
# importing the module mod.pyimport mod            # creating a object jack with jack as name and 21 as agejack = mod.Age('jack','21')   # calling the getAge() method for object 'jack'jack.getAge()                
Output:
The age of jack is 21
The way we imported only a particular function in the same way we can import only a particular class. The code for this will be:
Python
# importing only the Age class from modfrom mod import Age            # creating the object jack with jack as name and 21 as agejack = Age('jack','21')      # calling the getAge() method for the object jackjack.getAge()                
Output:
The age of jack is 21
For getting any variable from a module we have to first import the module, and then we can simply get it by module_name.variable_name. For example, if we want to print the variable with name s present in our mod.py module we will write the following code:
Python
# importing the module mod.pyimport mod            # printing the variable s present in mod.pyprint(mod.s)        
Output:
I love python!
As we did for functions and classes in the same way we can import a particular variable from a module. For printing variable ‘s’ by this method the code will be:
Python
# importing only the variable s from module mod.pyfrom mod import s           # printing the variable sprint(s)    
Output:
I love python!
Picked
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python