Package in Python
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
May be interested
- Module in Pythonin this python lesson we will know how to create and import a module in python. we will learn a few ways to import, using custom modules as well as python's built-in modules, please watch.
- Global keywords in Pythonwhat does the global key do and how do i use it in python? please follow us.
- Global variables (global), local variables (local), nonlocal variables in Pythonin this python lesson you will learn about global variables, local variables, nonlocal variables in python and the use of these variables.
- Anonymous function, Lambda in Pythonwe have gone halfway through the python function, in this lesson you will learn about anonymous functions, also known as lambda functions. what is lambda jaw, how is lambda function syntax and how to use lambda function with specific example.
- Recursive function in Pythonin the previous articles, you learned about python functions, built-in python functions, and user-defined python functions. in this article we will learn more about recursive functions in python, which call itself, as well as how to create recursive functions and examples.
- Python function parameterin the previous article we learned about the built-in python function and the user-defined python function with customizable number of parameters. you will know how to define a function using the default parameters, keyword and custom parameters in this article.