Modules in Python

Search for a command to run...

No comments yet. Be the first to comment.
YAML Ain't a Markup Language. This is the full form of YAML. Why YAML ? YAML is a serialization language. Means that it is used to transfer the data from one end to other like from the backend to the frontend and it is also used to store data in k...

Step 1: Install it sudo npm i -g npkill Or you can use it without installing with npx npx npkill Step 2: Go the directory where you want to scan for node_modules folder npkill # if you have installed it npx npkill # if you have not install...

Classes All the functions in a class should have at least one parameter and by default, we call it self. self is a reference to the current object # CREATEING CLASS class Point: def draw(self): print("draw") # CREATING OBJECT FROM THE...

This is not like pop notifications. This is more like a customized alert notification. Click here to visit the official GitHub page of this project. How to use react-toastify Step 1: Install it npm i react-toastify Step 2: Import it in app.js ...

As our program grows we should split our code across multiple files. We refer to each file as a module. A module should contain related data, functions, and objects.
def calc_tax():
pass
def calc_shipping():
pass
Now we can import those functions to this file.
# import
from sales import calc_tax
# use it
calc_tax()
# ==============================
# import multiple functions
from sales import calc_tax, calc_shipping
# ==============================
# import everything
from sales import *
another way of importing
# import
import sales
# use
sales.calc_tax()
Even if we imported only one function then also the entire module needs to be loaded.
When we will compile the app.py file using the command python app.py then python will create a folder names __pycache__
In this folder, there will be a compiled version of the module that you just imported in the app.py file.
This is done to speed up compilation. And this will speed up the loading of the module.
This will not speed up the performance of the program.
app.py is not cached because the entry file is always recompiled.
If the python compiler does not find the module in the current folder then it will search for the file in a bunch of predefined folders.
The built in modules like sys are kept in these folders.
Packages are used if the file that we want to use is inside a sub folder.
The folder structure looks like this.

We can not do this -
# we can not do this
from ./ecommerce/sales import tax
We have to create a __init__.py file in the ecommerce folder. When we create this file then python will think of this folder as a package.
A package is a folder that contains one or more modules.
So a module is a file and a package is a folder.
The __init__.py file is empty.

Now you can import and use it in three ways
# way 1
import ecommerce.sales
ecommerce.sales.tax()
# =========================
# way 2
from ecommerce.sales import tax, sales
tax()
# =========================
# way 3
import sales from ecommerce
sales.tax()
Now we have to create another __init__.py file in the sub folder also
Our folder structure is like this
now import like this
from ecommerce.shopping import sales
This simply means, using one package into another package.

In our sales modules, we want to use the contact module.
we can use absolute import
from ecommerce.customer import contact
contact.customer()
we can use relative import
from ../customer import contact
contact.customer()
This function will give us all the functions that are defined in a module
from ecommerce.shopping import sales
print(dir(sales))
some functions such as__name__ , __package__ , etc are already defined for us.
print(__name__)
print(__package__)
print(__file__)
'''
These functions give us the file name of the module,
the package name and the address of the module
'''
The name of the module that starts our program is always
__main__