Module in Python

Source: Internet
Author: User

Module is an important concept in Python. A common case is to write a statement in advance. py file, which will be written in advance when the import is required in another file. copy the py file to the current directory, or in SYS. add the pre-written. directory where the py file is located, and then import. This method is feasible for a few files, but ifProgramA large number of layers are complex and difficult.
Is there a way to organize multiple. py files like a Java package so that they can be called externally and internally? The answer is yes.

To understand this problem, you must first know what operations Python performs when executing the Import Statement. According to the python documentation, it performs the following operations:
Step 2: create a new, empty module object (which may contain multiple modules );
Step 2: insert the module object into SYS. module.
Step 2: load the moduleCode(Compilation is required first)
Step 2: Execute the corresponding code in the new module.

When executing step 1, you must first find the position of the module program. The principle is as follows:
If the name of the module to be imported is M1, the interpreter must find m1.py, which is first searched in the current directory and then in the environment variable pythonpath. Pythonpath can be regarded as a class of system PATH variables, including several directories. If pythonpath is not set or m1.py cannot be found, search for the default path related to Python installation settings. in UNIX, it is usually/usr/local/lib/python.
In fact, the search order is: the current path (and the specified SYS. path from the current directory), then the pythonpath, and then the default path related to Python installation settings. Because of this order, if the current path or pythonpath contains the same module as the standard module, the standard module will be overwritten. That is to say, if XML. py exists in the current directory, the module under the current directory is imported when the import XML is executed, rather than the system standard XML.

With this understanding, we can build a package and import it as a common module to directly access each module in the package.

The package definition in python is very simple. Its hierarchy is the same as that in the directory where the program is located. This is similar to Java. The only difference is that, the package in Python must contain _ init __. py file.
For example, we can organize a package as follows:

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. If it exists, it indicates that the directory should be processed as a package. Of course, you can set the corresponding content in __init _. py, which is described in detail below.

Now we define a function in module_11.py:

Def FUNA (): Print "funca in module_11" Return

Run python in the top-level directory (that is, the directory where package1 is located, and, of course, refer to the above introduction to place package1 where it can be searched by the interpreter:

 
>>> From package1.subpack1. module_11 import funca >>> funca () funca in module_11

In this way, the functions in module_11 are correctly called according to the package hierarchy.

Careful users will find that sometimes a wildcard * appears in the Import Statement to import all elements in a module. How can this be achieved?

The answer is in _ init _. py. In subpack1's _ init _. py file, write

 
_ All _ = ['module _ 13', 'module _ 12']

Then go to Python

 
>>> From package1.subpack1 import *> module_11.funca () traceback (most recent call last): file "<stdin>", line 1, in <module> importerror: no module named module_11

That is to say, when * is used for import, the module in the package is limited by _ init _. py.

Now let's take a look at how to call each other within a package.

If you want to call the module in the same package, you can directly import it. That is to say, in module_12.py, you can directly use

 
Import module_11

If it is not in the same package, for example, if we want to call funca in module_11.py in module_21.py, we should:

 
From package1.subpack1. module_11 import funca

Of course, there are simpler methods in Python 2.5:

 
From .. subpack1.module _ 11 import funca

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.