Table of Contents
This guide covers package in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
What Is a Package in Python?
Usually users do not store all their files on the computer in the same folder, location, but use the directory hierarchy for easier access. Similar or similar files related to a particular topic will be placed in the same directory, such as lessons on Python functions that will be stored in the Python function directory. Similarly, Python has directory and module packages for files.
When the program is coding more and more with many modules, we will put the same modules into a package, and other module groups into other packages. This makes it easier to manage the program and it is easier to understand.
If the directory can contain subdirectories, so does the package, in a package that can have sub-packages and other modules.
A directory must contain a file called __init__. Py to Python to understand that this directory is a package. This file can be left blank, but usually programmers often put initialization code for the package here.
Suppose, we are developing a program named QTM, with subsets, modules as follows:
Enter the Module from the Python Package
You can import modules from the package using the dot operator (. ).
For example, if you want to import the module mo. Py in the above example, you should do the following:
import QTM.Video.mo
If the mo. Py module contains a function named chon_video (), you will have to use the full name to refer to it:
import QTM.Video.mo.chon_video(video1)
If the above structure is too long, you can import modules without prefixing the package as follows:
from QTM.Video import mo
You can call simple functions like this:
mo.chon_video(video1)
In addition, there is another way to only enter the required function (class or variable) from the module in a package as follows:
from QTM.Video.mo import chon_video
Next, call this function directly:
chon_video(video1)
Although easier, this last entry is not recommended. Using a full name will help reduce confusion and avoid duplication between identical identifiers.
While importing packages, Python will search the directory list defined in sys. Path, just like the search path for the module.
Next article: Array in Python
Previous article: Module in Python
Don't forget to do Python exercises!
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.