Opening Hours :7AM to 9PM
In this article, you will learn to create and import custom modules in Python. Also, you will find different techniques to import and use custom and built-in modules in Python.
# Python Module example def add(a, b): """This program adds two numbers and return the result""" c = a + b print(c)Here, we have defined a function add() inside a module named example. The function takes in two numbers and returns their sum.
>>> import aThis does not import the names of the functions defined in example directly in the current symbol table. It only imports the module name example there.
>>> a.add(4,5.5) 9.5Python has tons of standard modules. You can check out the full list of Python standard modules and their use cases. These files are in the Lib directory inside the location where you installed Python.
import math print("The value of pi is", math.pi) Run Code When you run the program, the output will be: The value of pi is 3.141592653589793
import math as m print("The value of pi is", m.pi) Run CodeWe have renamed the math module as m. This can save us typing time in some cases.
from math import pi print("The value of pi is", pi) Run CodeHere, we imported only the pi attribute from the math module. In such cases, we don't use the dot operator. We can also import multiple attributes as follows:
>>> from math import pi, e >>> pi 3.141592653589793 >>> e 2.718281828459045
from math import * print("The value of pi is", pi) Run CodeHere, we have imported all the definitions from the math module. This includes all names visible in our scope except those beginning with an underscore(private definitions). Importing everything with the asterisk (*) symbol is not a good programming practice. This can lead to duplicate definitions for an identifier. It also hampers the readability of our code.
>>> import sys >>> sys.path ['', 'C:\\Python33\\Lib\\idlelib', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages'] We can add and modify this list to add our own path.
>>> import my_module This code got executed >>> import my_module >>> import my_moduleWe can see that our code got executed only once. This goes to say that our module was imported only once.
>>> import imp >>> import my_module This code got executed >>> import my_module >>> imp.reload(my_module) This code got executed <module 'my_module' from '.\\my_module.py'>
>>> dir(example) ['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', 'add']Here, we can see a sorted list of names (along with add). All other names that begin with an underscore are default Python attributes associated with the module (not user-defined).
>>> import example >>> example.__name__ 'example' All the names defined in our current namespace can be found out using the dir() function without any arguments. >>> a = 1 >>> b = "hello" >>> import math >>> dir() ['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']