Python is a programming language that has won hearts in the world over. From the coding community to the Data Science community, Python is an absolute favourite of all. The reason for its popularity is that Python comes loaded with a wide range of libraries and modules that make development a hassle-free task.
While we’ve previously talked about Python libraries at length, today, we’ll focus on Python modules.
What are Python Modules?
In simple words, a Python module is a Python object consisting of arbitrarily named attributes that can be used for both binding and reference. Essentially, a module can define functions, classes, and variables. Modules help you to organize Python code logically. By grouping related code into modules, you can make Python code more easy-to-use and understand.
In Python, you can define a module in three ways:
- You can write a module in Python.
- You can write a module in C and load it dynamically at run-time.
- You can use built-in Python modules that are intrinsically contained in the interpreter.
What is Module Search Path?
The search path refers to a list of directories that the interpreter searches before it can import a module. Let’s say, you want to execute the statement:
import mod
When the interpreter executes this statement, it will search for mod.py in a list of directories assembled from multiple sources, including:
- The directory from which you ran the input script or the current directory (provided the interpreter is running interactively).
- If the PYTHONPATH environment variable has been set, it will search the list of directories contained in it.
- The list of installation-dependent directories that are configured while installing Python.
You can access the resulting search path using the Python variable sys.path that is further produced from the sys module:
>>> import sys
>>> sys.path
[”, ‘C:\\Users\\john\\Documents\\Python\\doc’, ‘C:\\Python36\\Lib\\idlelib’,
‘C:\\Python36\\python36.zip’, ‘C:\\Python36\\DLLs’, ‘C:\\Python36\\lib’,
‘C:\\Python36’, ‘C:\\Python36\\lib\\site-packages’]
Once you import a module, you can determine its location using the __file__ attribute of the module, like so:
>>> import mod
>>> mod.__file__
‘C:\\Users\\john\\mod.py’
>>> import re
>>> re.__file__
‘C:\\Python36\\lib\\re.py’
However, keep in mind that that directory portion of the __file__ should be a directory contained in sys.path.
Now that you have understood the essence of Python modules, let’s take a look at some of the best Python modules.
Explore our Popular Data Science Courses
Check out our data science courses to upskill yourself.
Top Python Modules
1. The “import” statement
By executing an import statement in one Python source file, you can use any Python source file as a module. The syntax of the import statement is:
import module1[, module2[,… moduleN]
When you run an import statement, the interpreter will import the module provided if it is present in the search path. For instance, if you wish to import the module calc.py, you must write and execute the following command:
# importing module calc.py
import calc
print add(10,2)
On successful execution of this command, the output will be as follows:
12
An important thing to remember about Python modules is that no matter how many times you import a module, it will be loaded only once. This helps to prevent repeated module execution in the case of multiple imports.
Check out All Python tutorial concepts Explained with Examples.
2. The “from…import” statement
In Python, the “from…import” statement allows you to import specific attributes from a module. Here’s an example of the “from…import” statement:
from modname import *
# importing sqrt() and factorial from the
# module math
from math import sqrt, factorial
# if we simply do “import math”, then
# math.sqrt(16) and math.factorial()
# are required.
print sqrt(16)
print factorial(6)
On running this code, you will get:
4.0
720
Using this module, you can import all the items contained within a particular module into the current namespace.
3. The “dir()” function
In Python, dir() is a built-in function that returns a sorted list of strings containing the names of all the modules, functions, and variables that are defined in a module. Given below is an example of the dir() function:
#!/usr/bin/python
# Import built-in module random
import random
print dir(math)
On execution, this code will return the following result:
[‘BPF’, ‘LOG4’, ‘NV_MAGICCONST’, ‘RECIP_BPF’, ‘Random’,
‘SG_MAGICCONST’, ‘SystemRandom’, ‘TWOPI’, ‘WichmannHill’,
‘_BuiltinMethodType’, ‘_MethodType’, ‘__all__’,
‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’,
‘__package__’, ‘_acos’, ‘_ceil’, ‘_cos’, ‘_e’, ‘_exp’,
‘_hashlib’, ‘_hexlify’, ‘_inst’, ‘_log’, ‘_pi’, ‘_random’,
‘_sin’, ‘_sqrt’, ‘_test’, ‘_test_generator’, ‘_urandom’,
‘_warn’, ‘betavariate’, ‘choice’, ‘division’,
‘expovariate’, ‘gammavariate’, ‘gauss’, ‘getrandbits’,
‘getstate’, ‘jumpahead’, ‘lognormvariate’, ‘normalvariate’,
‘paretovariate’, ‘randint’, ‘random’, ‘randrange’,
‘sample’, ‘seed’, ‘setstate’, ‘shuffle’, ‘triangular’,
‘uniform’, ‘vonmisesvariate’, ‘weibullvariate’]
In the output given above, while the special string variable __file__ points to the filename from which the module was loaded, __name__ becomes the module’s name.
upGrad’s Exclusive Data Science Webinar for you –
Transformation & Opportunities in Analytics & Insights
Read our popular Data Science Articles
4. The globals() and locals() functions
You can use the globals() and locals() functions to return module names in the global and local namespaces. This, however, depends on the location from where you call the names. If you call the globals() function within another function, it will return all the names that can be accessed globally from that particular function. On the contrary, if the locals() function is called from within a function, it will produce all the names that you can access locally from the specific function.
Top Data Science Skills to Learn
Top Data Science Skills to Learn | ||
1 | Data Analysis Course | Inferential Statistics Courses |
2 | Hypothesis Testing Programs | Logistic Regression Courses |
3 | Linear Regression Courses | Linear Algebra for Analysis |
5. The reload() function
Generally, when you import a module into a script, the code present at the top-level portion of a module will only be executed once. In this situation, if you wish to re-execute the top-level code in a module, the reload() function is the go-to function. This function allows you to re-import a previously imported module.
The syntax of the reload() function is as follows:
reload(module_name)
In the syntax, the module_name refers to the name of the module you wish to reload – it does not pertain to the string containing the module name. For instance, if you want to reload the hello module, you must write:
reload(hello)
Conclusion
In Python, packages and modules are interrelated. Python packages facilitate hierarchical structuring of a module namespace using dot notation. While Python packages prevent collisions (overlaps) between module names, Python modules prevent collisions between global variable names.
If you are curious to learn about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.