In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of packages and modules in Python", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of packages and modules in Python".
What are the packages and modules of Python
Package definition: a simple and rude explanation, when a folder contains a _ _ init__.py file, we can think of it as a package; if there is a subfolder under the folder and there is a _ _ init__.py file in the subfolder, we can think of it as a package.
Definition of module: any file in * .py format can be treated as a module, and we can import it using the import keyword.
We all know that there are Python functions under each module, so we use a package or module, and the ultimate goal is to use the functions of their internal functions to help us meet our business needs.
There can be one or more modules in a package; it can be executed by calling a function of one of the modules below the package, which is why we use Python packages or modules
The ID card of the bag
The _ _ init__.py file is a file that must exist for every python package. The python interpreter will think that this is a package only if there is a directory where the _ _ init__.py file exists; without this file, the python interpreter will only think that this is a normal folder. The schematic diagram is as follows:
As we can see from the image above, in addition to the _ _ init__.py file, the directory file icon of the package has a round dot pattern. The normal folder does not have any identification. Imagine if we created a _ _ init__.py file in the python_package_test folder. 44944...
How to create a package
In fact, there is no need to say anything more, first create a folder. Then create an ID file _ _ init__.py of the python package in the folder; the _ _ init__.py file can be empty and you don't have to write anything, just make sure the name of the file is _ _ init__.py. The Python explanation will think that this is a package, and then we can write any script (that is, module) in the package, which is very simple. However, we also need good practices for creating packages:
First of all, there should be a theme, the function is clear, but also easy to use; it is convenient for others to know if they need to use your package at a glance.
Secondly, there should be a clear hierarchy. There can be many modules or subpackages in a package. The relationship between them should be clear, which also plays a good auxiliary role in later maintenance code.
In addition to the above identity file _ _ init__.py, which creates a package after creating a folder, Pycharm can also create a package directly through new-- > Python Package. The schematic diagram is as follows:
A little exercise for creating a package
Next we try to create a package for animal, as well as a cat package and a dog package under the animal package. Then create an action.py module in the cat package and in the dog package, and define the eat (), jump (), and run () functions in their respective action.py modules.
The same method is used to create an action.py module in the dog subpackage, as follows:
# coding:utf-8def eat (): return 'dogs love to gnaw on bones' def run (): return 'cats can run' def jump (): return 'cats can jump'
Here we have created two subpackages, cat and dog, and what is really functional is the action.py module in the two subpackages. In each of these two modules, there are three functions eat (), run (), and jump (). So our ultimate goal is to use the three functions of the action.py module from the cat and dog subpackages, respectively, and then let's look at how to import, call, and use the functions in the subpackages.
Import of packages-import
If you want to use a package or module, you need to import it into our current script first. In the chapter of learning the structure of the python script, we have introduced that at the top of the script is the header comment area; then there is the import area, which is to import the packages and modules we need into our current script. Next, let's take a look at the use of the import import keyword for the package:
# usage: import package# parameter: # package: the name of the imported package # requirement: # only the python package can be captured and imported by the impor keyword, that is, the file _ _ init__.py must be available in the python package # and import will only get the functions in the _ _ init__.py in the current corresponding package or in the current module.
It may not be easy to understand that import will only get the functions in the _ _ init__.py under the current corresponding package or the functions in the current module. Let's do a small experiment in Pycharm to deepen our understanding.
First open the Terminal terminal and type python to start the Python interpreter. Then import the animal package, and then use the cat subpackage under the animal package. As shown below:
It is found here that there is an error, and the attribute error animal does not have the attribute cat; why? The subpackage cat is really under our animal package. To do a little experiment, we write something in the _ _ init__.py file under the animal package.
Then exit our python interpreter and enter again, why quit? This is because the current structure of our package has changed, so we need to exit and re-enter, otherwise the result executed at the Treminal terminal is still the same as before. At this time, we will re-import animal the package, and then execute the following to see.
At this time, when combining the import we just mentioned, we will only get the functions in the _ _ init__.py under the current corresponding package or the functions under the current module. If you want to call the function functions in a package or module, you only need to use the name of the package or module +. Just call its function name in the form of Is this approach similar to the way we use object-oriented?
Now there is another question. We just imported the animal package and used the function in the _ _ init__.py file, so how can we use the function function in the cat / dog subpackage under the animal package? Moving on, let's look at the import part of the module
Module import-from … Import
Next we learn how to import the corresponding module through the package, this time we need two keywords from and import to match; when these two keywords work together, we can find the corresponding module through a package. Next let's take a look at from...import.... The usage of
# usage: from package import module# parameter: # package: package name to import # module: the target module in the package # from package import module means to import a module from the current package # example is as follows: from animal import dogdog.run () # through from. Import... Directly found the dog module # so only need to use the dog module directly. To find the run method inside and execute it
Next, we try to import the run function in the subpackage call of dog according to the animal package above, as follows:
Import subpackage and call of subpackage function
Here we found a problem, there are action modules under the two subpackages, which will be overwritten when we import and call separately. Is there any way for us to import the action modules of the two subpackages at the same time, which can be called separately but do not affect each other? This requires the help of our as keyword. When we learn about exceptions, we use the as keyword keyword to alias the caught exception, where we can still alias the module we imported. The figure below is as follows
Now we are directly through the import module to call the function under the action module, can we directly call the function to use it? Try it:
Import the main package and the function calls of the main package
First we create a main.py file under the subpackage sibling path of the main package animal, define a main function, and then call it. The contents are as follows:
# coding:utf-8def animal (): return 'this is a function of the module under the sibling path of the subpackage under the animal package'
Then we call the animal function of the main.py file
How to optimize the excessive length between imported package and subpackage module
Let's take a look at the import of the eat () function of the action module of the cat subpackage under the animal package.
We mentioned earlier that import will only get the functions in the _ _ init__.py under the current corresponding package or the functions in the current module, so can we import the functions in the cat and dog subpackages into the _ _ init__.py under the animal package?
Next, let's try to create an animal_test.py file at the same level as the animal package, and then import the subpackage into the animal_test.py file to get its functions.
Note: the animal_test.py script here needs to be added to the file at the same level as the animal package before you can import the function of the defined _ _ init__.py file; if it is not at the level of the same file, the import is not successful, you can try it manually.
Powerful third-party package
Above we learned what a python package is and how to create a package and use our packages and modules through import. The use of the package above is equivalent to our own development and use, which belongs to a self-produced and self-selling model. Next, we will learn to use third-party packages developed by others to help us work efficiently. after learning this chapter, we can choose the packages we need from the vast third-party packages to help us complete the functions we want.
For example, the crawler's package can help us crawl pictures, articles and other data we want; the drawing package can help us deal with all kinds of pictures.
What is a third-party package
In fact, third-party packages are packages or modules that other programmers disclose to the Python designated website to write functions into packages or modules, which are convenient for other programmers to download and use.
The biggest advantage of using third-party packages is that the functions we want to achieve may have been written for us, and we just need borrowlism and apply them directly to our own code, which naturally greatly improves our development efficiency.
How to install a third-party package
After learning about third-party packages, we need to download and install them before they can be used in our code. Pip and easy_install are the tools for obtaining third-party packages. When we install python, pip is installed automatically. We only need to use it on the Terminal terminal.
With the rapid development of python, the utilization rate of pip is getting higher and higher, so it gradually replaces easy_install.
As we just said, when we install python, there are version restrictions on installing pip automatically, and pip will not be installed before Python 3.4; after Python 3.4, we will come with these two package management tools.
If you are using an older version of python, you can download and install pip through https://pip.pypa.io/en/stable/installing/
Since there are many more third-party packages in pip management tools than easy_install, we focus on the use of pip. The use of pip is also very simple, we just need to enter the pip install package name in the Terminal terminal to download and install it.
We also recommend a treasure trove of python programmers, github.com searches for python third-party packages (github will accompany the programmer's full career); there are many third-party packages made public by programmers in github, and naturally there are python's. It should be noted that github is a foreign website, so the visit is a little slow, you can ke, xue, shang, wang. Or you can temporarily use the domestic image source address, which is described in the pip installation and expansion section below.
Pip installs the third package
# query current pip version: pip-V# execution result is as follows: # pip 21.1.2 from D:\ PycharmProjects\ XXXXX\ XXXXX\ venv\ lib\ site-packages\ pip (python 3.10)
PS: each version of python has its own pip, so when we install multiple versions of python, we hesitate to have too many python versions, so if we use the wrong pip, it will sometimes look like we have a third-party package installed, but can't use it. So we must pay attention to this place!
Install the first third-party package tool-ipython
Next let's try to install our first third-party package, ipython.
Ipython is an interactive shell of python, which is much easier to use than the default python shell. It supports automatic completion and indentation of variables.
In the Terminal terminal, enter pip install ipython, and if you report an error: WARNING: You are using pip version 21.1.2; however, version 22.0.4 is available. You can install or temporarily use the domestic mirror source address through ke, xue, shang, or wang, or upgrade your pip; upgrade command python-m pip install-upgrade pip
Here we see that our installation failed; install again using the methods we mentioned above, such as ke, xue, shang, wang.
When we see Successfully, it means that we have successfully installed it. Although we are still prompted by WARNING to upgrade pip, we still install it successfully. Try to see if ipython works properly.
Compared with the shell terminal of python, ipython not only interacts more conveniently, but also automatically supports automatic completion of variables and automatic indentation. It highlights the great convenience of humanization.
It is also important to note that when our environment installs multiple versions of python, be sure to check that we have installed the correct path, otherwise it will be installed to other versions of python. As shown below:
Of course, if the default is to enter our other version of python or ipython, we can enter the interactive terminal of the version I want to enter through the command of python3 or ipython3.
Note: when we usually execute a script, we still recommend using the python interpreter. Ipython is only used as one of our debugging environments.
Pip installation extension
In fact, we have a choice to install pip and third-party packages. By default, we choose a foreign address to download and install, which is often very slow due to network and distance. At the same time, our installation will fail because of network fluctuations.
In this case, we have launched an address that can be downloaded and installed nearby, such as our Tsinghua University, Aliyun, China University of Science and Technology, Huazhong University of Science and Technology, and so on. As follows:
# official source address # https://pypi.python.org/simple/# https://pypi.tuna.tsinghua.edu.cn/simple/# domestic source address # Aliyun: http://mirrors.aliyun.com/pypi/simple/ # University of Science and Technology of China: https://pypi.mirrors.ustc.edu.cn/simple/# Douban: http://pypi.douban.com/simple/# Tsinghua University: https://pypi. Tuna.tsinghua.edu.cn/simple/# University of Science and Technology of China: http://pypi.mirrors.ustc.edu.cn/simple/# is used as follows: pip install-I http://pypi.douban.com/simple/ ipythonpip install-I http://pypi.douban.com/simple/ ipython==7.12.0 # specified version number to install
Uninstall the third-party package for pip installation
Since you can install it, you can definitely uninstall it. The uninstall method is as follows:
# Uninstall method: pip uninstall ipython# also needs to pay attention to the problem of the python interpreter version corresponding to the uninstalled third-party package.
The above is all the contents of the article "sample Analysis of packages and Modules in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.