Swipe left or right to navigate to next or previous post
This blog post is about the python, modules and packages
Python is one of the most popular programming language created by Guido van Rossum. It was first released on 1991.
It is object-oriented high-level interpreted and dynamic typing programming language. It has built-in dynamic data structures
which supports dynamic binding. It is easy to learn.
Python is used in everything from building the production-ready web pages to system scripting, in software development to complex mathematics,
from big data to perform complex mathematics. It is used in the self-controls cars to the Netflix's recommendation
algorithm. Being the general purpose language, it is used in the wide range of applications that includes data science,
machine learning, automation, software development, web development, automate tasks, conduct data analysis.
Module is any python file with .py extension containing the python code. Different python objects such as classes,
variables, constants, functions can be defined in the module. Module organizes these objects in logical way .
All the python objects are available to an interpreter session or another python module by importing
the module using the import statement. Functions defined in the modules needed to be imported before using. A module can be created by creating a text file with .py extension. A module to multiply a number can be
saved as mul.py file name with following code
There are three different ways to define a module in python
A module can be imported in another python module by importing the module. Following code can be used to import the mul file in the add.py file This will multiply the 5 and 2 and returns its result. Module can be imported as alias using as keyword A module can be executed just as it is imported. But it needs the __name__ set to __main__ which means we need to
add the following code at the end of the module You can run the mul module with two params as follows in a terminal A package contains one or more relevant modules organized in the logical way. The package folder must contain the special python file called __init__.py. The __init__.py has two purposes. The __init__.py can be normally kept empty. However, it contains import statement that import specific funtions
from modules to available for import packages and modules. This section describes what happens when we execute the following python statement
When we execute the above statement, the python interpreter searches the mul.py in a list of directories which are
assembled from the following sources.
The lists of directories for searching the packages in the system can be obtained from the sys moduleWhat is Python?
pros of python
What can you do with python?
Why Python?
Why Python is so popular?
Modules in Python
Creating a Module
def mul(a, b):
return a*b
Different ways to define a module in python:
Importing a Module
import mul
print(mul.mul(5,2))
import mul as multiply
print(multiply.mul(5,2))
Executing modules as scripts
if __name__ == "__main__":
import sys
mul(int(sys.argv[1]), int(sys.argv[2]))
python mul.py 5 2
Packages in Python
__init__.py
from mul import mul
The Module Search Path
python mul.py 5 2
import sys
sys.path