Import and use of custom packages python

 https://blog.csdn.net/maryhuan/article/details/22048763

Built-in modules can be viewed through the built-in function in Python "dir (__ builtins__)"

Package (packages) is actually a module type is Type module. There are two methods generally referenced custom module when:
1) the two files in the same directory.
2) add the path to the referenced file py in sys.path. Then import.
This approach is feasible for a small number of files, but if the number of procedures a lot, very complex hierarchy more trouble. At this point you will be able to organize more py file with package together, similar to the same reference third-party packages. To a lot easier.

the same hierarchy of the directory hierarchy of the package and program, and it must contain a __init__.py file. __init__.py can be empty, as long as it exists to show that this directory is used as a package deal.

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, it indicates this directory should be as a package deal. Of course, __ init__.py the corresponding content may be provided.
Well, now we define a function in module_11.py:

DEF Funa ():
Print "funcA in module_11"
return


a reference module.
In the top-level directory (ie the directory where package1, of course, refer to the above description, the package1 on the interpreter be able to search the place) run Python:

>>> from package1.subPack1.module_11 Import funcA
>>> funcA ()
funcA in module_11

this way, we follow the hierarchy of the package, correctly calls a function in module_11 .


II. Use the wildcard *, import all the elements of a module of
the answer lies in the __init__.py. We write in subPack1 the __init__.py file

__all__ = [ 'module_13', ' module_12']

and then enter the Python
>>> from package1.subPack1 Import *
>>> module_11.funcA ()
Traceback (MOST recent Results Last Call):
File "", Line 1, in
ImportError: No Module named module_11

in other words, in order to * import, module in the package is subject to __init__. py restrictions.

3. In the interior package call each other.
1. If you want to invoke the same package in the module, you can directly import. In other words, module_12.py, you can directly use the
Import module_11

2. If not in the same package, for example, we want to module_11.py in FuncA call module_21.py, you should like this:
from module_11 package name .module_11 import funcA

four .Python how to find the module we defined?
in the standard package sys the path attribute records the package path of Python.
SYS Import
Print (sys.path)
we can usually be placed in the module package path PYTHONPATH environment variable, the variable is automatically added to the environment sys.path attribute.
Another convenient method is specified in the programming of our direct path module to sys.path in.
Commonly used words can be placed python27 \ lib \ site-packages folder.

----------------
Disclaimer: This article is the original article CSDN bloggers "Maryhuan", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/maryhuan/article/details/22048763

Guess you like

Origin www.cnblogs.com/jodyccf/p/11906768.html