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.
Table of Contents
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:
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
[”, ‘C:\\Users\\john\\Documents\\Python\\doc’, ‘C:\\Python36\\Lib\\idlelib’,
‘C:\\Python36\\python36.zip’, ‘C:\\Python36\\DLLs’, ‘C:\\Python36\\lib’,
Once you import a module, you can determine its location using the __file__ attribute of the module, like so:
>>> import mod
>>> import re
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.
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
On successful execution of this command, the output will be as follows:
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.
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.
On running this code, you will get:
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:
# Import built-in module random
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.
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.
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:
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:
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.
What is Python Anaconda and why is it so popular?
Anaconda is a package manager for Python and R. It is considered to be one of the most popular platforms for data science aspirants. The following are some of the reasons that get Anaconda way ahead of its competitors. Its robust distribution system helps in managing languages like Python which has over 300 libraries. It is a free and open-source platform. Its open-source community has many eligible developers that keep helping the newbies constantly. It has various AI and ML-based tools that can extract the data from different sources easily. Anaconda has over 1500 Python and R data science packages and is considered to be the industry standard for testing and training models.
Name some of the most popular Python libraries for image processing.
Python is the most suitable language for image processing due to the feature-rich libraries that it provides. The following are some of the top Python libraries that make image processing very convenient. OpenCV is hands down the most popular and widely used Python library for vision tasks such as image processing and object and face detection. It is extremely fast and efficient since it is originally written in C++. The conversation over Python image processing libraries is incomplete without Sci-Kit Image. It is a simple and straightforward library that can be used for any computer vision task. SciPy is majorly used for mathematical computations but it is also capable of performing image processing. Face Detection, Convolution, and Image Segmentation are some of the features provided by SciPy.
Why do most data scientists prefer Python over other languages?
There are many languages like R and Julia that can be used for data science but Python is considered to be the best fit for it due to many reasons. Some of these reasons are mentioned below: Python is much more scalable than other languages like Scala and R. Its scalability lies in the flexibility that it provides to the programmers. It has a vast variety of data science libraries such as NumPy, Pandas, and Scikit-learn which gives it an upper hand over other languages. The large community of Python programmers constantly contributes to the language and helps the newbies to grow with Python.