Package in Python
We will learn how to divide code into efficient, clear modules, using Python packages. Plus, the way to import and use your own package, or the package you download from somewhere into the Python program.
In the previous lesson, you learned about modules, how to create and call a module in Python. In this article, we will learn how to divide code into efficient, clear modules, using Python packages. Plus, the way to import and use your own package, or the package you download from somewhere into the Python program.
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
We 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
We 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
Then 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!
You should read it
- Module time in Python
- More than 100 Python exercises have solutions (sample code)
- Manage files and folders in Python
- Module in Python
- For in Python loop
- 5 choose the best Python IDE for you
- Bookmark 5 best Python programming learning websites
- Python data type: string, number, list, tuple, set and dictionary
- How to Open a Python File
- What is Python? Why choose Python?
- Why should you learn Python programming language?
- How to install Python on Windows, macOS, Linux
Maybe you are interested
This program will help you become invisible before the webcam lens is recording live Brain research explains the difference in the impact of technology on men and women How to reduce PDF file size by software 18 'extremely creative' inventions are available only in Japan 'Eyes glued' to the screen before going to bed is not harmful to the mental health of the bar, teenager! Can ants survive if they fall from the roof of the building?