In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
How to understand the difference and use of Python modules, packages, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
I. definition of modules and packages
Definition of module: any * .py file can be imported as a module using import
Definition of package: a directory containing a _ _ init__.py and other modules and other subpackages
What do the so-called packages and modules represent in the actual project, respectively, as follows:
A bag means test.
The module is do_excel.py,http_request.py,run.py.
II. Various methods of importing packages
Let's use the directory above to explain how to import packages in the run.py file.
Import a single test package
Import test
Import report, log packages
# Import at the same time, separated by commas, import report and log are recommended for wall cracks
# Import import reportimport log separately
Import a single test package and alias it
Import test as t
Import report and log packages and give them aliases
Import report as r, log as l
Import old package under project package
Import project.old
Import test package under old package under project package
# method 1: chain call import project.old.test
# method 2: from.. Import.. from project.old import test
Let's sum up the knowledge points here.
The package name followed by import is also the name of the unit variable (easy to understand by yourself)
What does it mean? Take the test package under the above old package as an example
Scenario: suppose I want to call the run () method in test.py under the test package
If you use method one, you have to write it this way.
Project.old.test.test.run ()
In the second way, it is written like this.
Test.test.run ()
You can see what "variable" is followed by import. When you want to call something in a package or module, you have to write "variable" before calling the package name and module name.
We can take a look at the following summary.
Import: full import
From.. Import..: partial import (targeted import)
How to distinguish the meaning of them? Let's take a look; the run.py code under the test package in the root directory is as follows
#! / usr/bin/env python
#-*-coding: utf-8-*-
Num = 1
Floats = 2.22
Strs = "string" def sum ():
Print ("I am sum")
Class test ():
Def test (self):
Print ("test class")
Suppose you want to call variables, function names, and class names under run.py in other files in the project
Import variables, function names and class names of other modules
Method 1: import mode
Everything in the run module (including variables, functions, class names) is imported, but it still has to be called unlinked through test.run
Import test.run# calls the num variable print (test.run.num) # output 1
# call the sum method print (test.run.sum ()) # output I am sum
Some friends just want to ask, can't I specify variables, function names, and class names? Let's try it.
#! / usr/bin/env python
#-*-coding: utf-8-*-
Import test.run.num
# call the num variable print (test.run.num)
First of all, if you are using Pycharm, the last num in import will be yellow, and there will be a prompt No module named num on hover.
Then, if you run the file, the error will be as follows
Traceback (most recent call last):
File "F:/test/tests.py", line 5, in
Import test.run.num,test.run.floats
ModuleNotFoundError: No module named 'test.run.num';' test.run' is not a package
The general meaning of the error is: test.run.num is not a module name, test.run is not a package
You can see that the complete import of import means that the smallest unit of import is the module, not the variable, function name, or class name.
Method 2: from.. Import..
# from package name. Module name import variable / function / class name from test.run import num from test.run import sumfrom test.run import testssfrom test.run import floats
If _ _ name__ = ='_ _ main__':
Print (num)
Print (sum ())
Print (testss () .test ())
Print (floats)
Execution result
one
I'm sum.
Test class
2.22
You can see from.. Import.. The import minimum unit can be a module, a variable, a function name, or a class name
Fourth, the method of importing module
According to the knowledge mentioned above, there are two ways to import a module, one is the import module name and the other is the from package name import module name
Import the run module under the test package
# importimport test.run
# fromfrom run import test
Import run and tests modules under the test package
# importimport test.run,test.tests
# fromfrom run import test,tests
Special knowledge
Question: where are import packages / modules, packages and modules imported from?
Answer: we searched in order through the list of paths included in sys.path.
How do you understand it?
Click the following code under the Pycharm project
Import sysprint (sys.path)
Execution result
['F:\\ moocInterface\\ test', 'F:\\ moocInterface', 'D:\\ PyCharm 2019.3.1\\ plugins\\ python\\ helpers\\ pycharm_display', 'D:\\ python3.6\\ python36.zip', 'D:\\ python3.6\\ DLLs', 'D:\\ python3.6\\ lib', 'D:\\ python3.6', 'D:\\ python3.6\\ lib\\ site-packages' 'D:\\ python3.6\\ lib\\ site-packages\\ django-2.1.5-py3.6.egg','D:\\ python3.6\\ lib\\ site-packages\\ pytz-2018.9-py3.6.egg','D:\\ PyCharm 2019.3.1\\ plugins\\ python\\ helpers\\ pycharm_matplotlib_backend']
You can see:
1. The path of the current file is placed first.
two。 The engineering path is in the second place.
3.Pycharm related paths are placed in the third place.
We enter the Python interpreter from the system cmd and type the following code to see the result
> import sys > print (sys.path)
The results are as follows
['','D:\\ python3.6\\ python36.zip', 'D:\\ python3.6\\ DLLs', 'D:\\ python3.6\\ lib', 'D:\\ python3.6', 'D:\\ python3.6\\ lib\\ site-packages', 'D:\\ python3.6\\ lib\\ site-packages\\ django-2.1.5-py3.6.egg' 'd:\\ python3.6\\ lib\\ site-packages\\ pytz-2018.9-py3.6.egg']
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.