call directory:

? SRC mod.py test.py

| |?? if in the program test.py mod directly into the module, using the

 import mod from mod import *

calls or subdirectories under the module:

 SRC mod.py | |??? Lib | | |? Mod2.py? 

the following documents

test.py from import * lib.mod2 directory structure or import lib.mod2

call of the directory:

? SRC |? Mod.py |? Lib | | |? Mod2.py? Sub test2.py import | |? Sys sys.path.append ('C:\test\ A\C') import mod import lib.mod2

__init__.py

in each package Python module, there is a __init__.py file (this file defines the properties and methods of the package) and then It is a number of module files and subdirectories, and if there is a __init__.py in the subdirectory, it is the subpackage of the package. When you import a package as a module (for example, import DOM from XML), you actually import its __init__.py file. A

package is a directory with a special file __init__.py. The __init__.py file defines the properties and methods of the package. In fact, it can be nothing to define; it can be just an empty file, but it must exist. If __init__.py doesn't exist, the directory is only a directory, rather than a package, it can't be imported or contains other modules and nested packets. There is an important variable in

__init__.py, called __all__.

if the Directory follows

? SRC | |? Mod.py? Lib | |? Mod2.py | |? Mod3.py | |? Sub | |? | |? Mod3.py

we sometimes resorted to a trick "imported", is this:

 from lib import *

and import __all__ in turn register the package in the __init__.py file in the list of sub modules and sub package into the current scope. For example:

 # file __init__.py __all__ = ["mod2", "MOD3", "sub"]

: summary for the rest of the path to download the PY file, import, first to sys.path, then import.

__init__.py

  • package: ID cannot delete the
  • definition in package __all__, used to write Python code into
  • fuzzy

  • (not recommended for writing Python module in __init__ can be in the package created in another module to write, try to ensure that __init__.py

summarized above is small to import and __init__.py Python introduce the basis of the introduction, I hope to help you, if you have any questions please give me a message, Xiao Bian will reply you timely. Thank you for your support for the home website of the script!


This concludes the body part