Source: http://www.cnblogs.com/tqsummer/archive/2011/01/24/1943273.html
The module in Python is a more important concept. A common scenario is to write a. py file in advance, copy the pre-written. py file to the current directory when you need the import in another file, or add the directory where the. py file is in the Sys.path, and then import. Such a practice is feasible for a few documents, but if the number of programs is large and the hierarchy is complex, it is very difficult.
Is there a way, like the Java package, to organize multiple. py files so that they can be called externally and internally? The answer is yes.
The concept of Python's package is mostly used,python __init__.pyPlays a more important role in the bag.
To understand this, you first know what exactly the python does when it executes the import statement, according to the Python documentation, which performs the following actions:
1th step, create a new, empty module object (it may contain multiple module);
2nd step, insert this module object into the Sys.module
3rd step, load the module's code (first must compile if necessary)
The 4th step is to execute the corresponding code in the new module.
When you perform step 3rd, you first find the location of the module program, which has the following principles:
If the name of the module that needs to be imported is M1, then the interpreter must find m1.py, which is first found in the current directory and then in the environment variable Pythonpath. Pythonpath can be thought of as a type of system path variable, which contains several directories. If Pythonpath is not set, or if m1.py is not found, the default path associated with Python's installation settings continues to be searched, under Unix, usually/usr/local/lib/python.
In fact, 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. Because of this order, the standard module is overwritten if the same module as the standard module exists in the current path or Pythonpath. That is, if xml.py is present in the current directory, then when the import XML is executed, the module in the current directory is imported instead of the system-standard XML.
With this in hand, we can build a package and import it as a normal module, so that we can access each module in the package directly.
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__.pyThe 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.
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 "<stdin>", line 1, in <module>
Importerror:no module named Module_11
In other words, the module in the package is subject to __init__.py restrictions when importing with *.
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
Reprint-Python inside about modules and packages and __init__.py some of the things