Reference: http://blog.sina.com.cn/s/blog_615c388d01017b5o.html
Note: Most of this article on the connection of the finishing, only for learning, hope Bo Master forgive me. Please attach the above link for reprint.
Why do you need a bag?
python Module is a more important concept. A common situation is to write a .py pieces, in another file need Import, will be written in advance .py file copy to the current directory, or in < Span style= "color: #ca0000;" >sys.path to add the .py files in the same directory, and then Import This is possible for a handful of documents, but if the number of programs is and the hierarchy is complex, it is very laborious. There is no way, like java package, will be more than .py" files are organized so that they can be called externally and internally, and within each other. The answer is yes.
Some mechanisms:
The first thing to know iswhat the Python does when it executes the import statement, according to the Python documentation, which performs the following actions:
Step 1, create a new, empty module object (it may contain multiple module);
Step 2, Insert the module object into the sys.module
Step 3, load the module code (first must compile if required)
Step 4, execute the corresponding code in the new module.
When you perform step 3, you first find the location of the module program, which has the following principles:
the order of the search is the current path (as well as the Sys.path specified from the current directory ), then PYTHONPATH, and then the default path associated with the installation settings for Python.
Definition of package:
The package definition in Python is simple, and its hierarchy is the same as the hierarchy of the directory in which the program resides, similar to Java, except that the packages in Python must contain a __init__.py file.
For example, we can organize a package like this:
package1/
__init__.py
subpack1/
__init__.py
module_11.py
module_12.py
module_13.py
subpack2/
__init__.py
module_21.py
module_22.py
......
__init__.py can be empty, as long as it exists, indicating that this directory should be treated as a package. Of course, __init__.py can also set the corresponding content, described in detail below.
OK, now we define a function in module_11.py:
Def Funa ():
Print "Funca in Module_11"
Return
In the top-level directory (that is, the directory where Package1 is located, and of course, refer to the introduction above, put Package1 where the interpreter can search) to run Python:
>>>from package1.subPack1.module_11 Import Funca
>>>funca ()
Funca in Module_11
In this way, we have correctly called the functions in Module_11 according to the package's hierarchical relationship.
Some questions to note about the package:
The careful user will find that sometimes a wildcard character * is found in the import statement, and all elements in a module are imported, how is this implemented?
The answer is in the __init__.py. We wrote it in the __init__.py file in SubPack1.
__all__ = [' module_13 ', ' Module_12 ']
And then into Python.
>>>from PACKAGE1.SUBPACK1 Import *
>>>module_11.funca ()
Traceback (most recent):
File "", Line 1, in
Importerror:no module named Module_11
In other words, the module in the package is subject to __init__.py restrictions when importing with *.
The package internally calls each other:
Well, finally, let's see how to call each other within the package.
If you want to call the module in the same package, you can import it directly. In other words, in module_12.py, you can directly use the
Import Module_11
If you are not in the same package, for example we want to call Funca in module_11.py in module_21.py, this should be the case:
From Module_11 package name. Module_11 Import Funca
Import of packages:
Method 1:
So how does python find the module we define? In standard package SYS, the Path property records the Python package path. You can print it out:
Import Sys
Print (Sys.path)
Usually we can put the module's package path into the environment variable Pythonpath, which is automatically added to the Sys.path property.
Method 2:
Another convenient way is to directly specify our module path to Sys.path in programming:
Import Sys
Import OS
Sys.path.append (OS.GETCWD () + ' \\parent\\child ')
Print (Sys.path)
About Python's package