Python Learning Notes-module Introduction (iii)-module package and search path

Source: Internet
Author: User

A python file is a module that uses a separate namespace, but it is clearly not enough to use modules to define Python functionality in practice. Because a large system of thousands of tens of thousands of modules is a normal thing, if all together is obviously not good management and there is a possibility of naming conflicts, so Python also appeared a concept of a package.

First, the package introduction in Python

A package is a way to create a Python module namespace by using the Point module name. GCA, the module name A.B represents a sub-module named B under a package named A. Just like using modules to keep authors of different modules from worrying about each other's global variable names (conflicts), the Point module name lets the author of a multi-module package do not have to worry about the module name (conflict) of each other. The representation of a module package in a file system is a collection of directories that form the hierarchical structure of the module package through the hierarchical structure of the directory, and the final module file is located in the final directory. For example, define a simple module under Package pkg, then perform the following steps:

1, in the D:\temp directory to create a directory, the name is pkg

2, under the PKG set up a __init__.py module, the content is empty can

3, in the PKG directory to create a Python module module_3.py, the contents are as follows:

#-*-encoding:utf-8-*-' pkg.module_3.py module contents ' ' Print ("Hello World") def func1 ():p rint ("This is Funciton one")

Note The 2nd step, you must set up a __init__.py module in each package directory, this is the Python rule, to tell the Python interpreter to use the directory as a content package, that is, the directory is a package containing the Python module. This is necessary, if not specified, we will report the following error when we import the module_3.py module in Python's interactive mode:

>>> Import Pkg.module_3traceback (most recent): File ' <stdin> ', line 1, in <module>importer Ror:no module named Pkg.module_3

So the __init__.py module in the package directory is required, but the content is optional, can be empty content, can also write some code or for other purposes. PVM will be piloted into the __init__.py module under this package when importing the module under a package, for example, we specify the contents of the __init__.py module as follows:

Print ("This is __init__ module")

Then re-import the package in interactive mode, the effect is as follows:

>>> import Pkg.module_3this is __init__ Modulehello world>>>

It can be seen that PVM first loads the __init__.py module and then the other modules in the directory are found and loaded.

Second, the module search path in Python

When a module is imported, PVM searches the module from a series of paths in the background with the following search process:

1, in the current directory search for the module;

2. In the list of paths specified in the environment variable pythonpath, search sequentially;

3. Search in the Python installation path

In fact, PVM searches through the path contained in the variable Sys.path, the list of paths contained in the variable is the path information mentioned above, and we can print to see which paths are included in the sys.pth:

>>> Import sys>>> Print (Sys.path) [', ' c:\\windows\\system32\\python27.zip ', ' d:\\tools\\python27 \\DLLs ', ' d:\\tools\\python27\\lib ', ' D:\\tools\\python27\\lib\\plat-win ', ' d:\\tools\\python27\\lib\\lib-tk ', ' D : \\tools\\Python27 ', ' d:\\tools\\python27\\lib\\site-packages ', ' d:\\tools\\python27\\lib\\site-packages\\ Wx-2.8-msw-unicode ']

The path information displayed on different machines may not be the same, but at least the 3 points mentioned above are included. Knowing this path search rule, we can easily add some directories dynamically to the search path, such as the e-disk set up a Python module module_4.py, the content is as follows:

Print ("Hello World") Print 2 * * 2

Then perform the import in interactive mode:

>>> Import Module_4traceback (most recent): File ' <stdin> ', line 1, in <module>importerror: No module named Module_4

The error is expected because the E disk is not in the search path of the Python module, and then we dynamically add the path to the search module, and then perform the import:

>>> sys.path.append ("e:\\") >>> print (Sys.path) [', ' c:\\windows\\system32\\python27.zip ', ' d:\\ Tools\\python27\\dlls ', ' d:\\tools\\python27\\lib ', ' D:\\tools\\python27\\lib\\plat-win ', ' D:\\tools\\Python27\\ Lib\\lib-tk ', ' d:\\tools\\python27 ', ' d:\\tools\\python27\\lib\\site-packages ', ' d:\\tools\\python27\\lib\\ Site-packages\\wx-2.8-msw-unicode ', ' e:\\ ']>>> import Module_4hello world4

The first is to add the Sys.path in the e-packing directory as the search path, the subsequent printing can be seen to have been added to the Sys.path, and then the import will normally import the module and execute the statements in the module. Of course, the newly added search path through the interactive mode is only valid in the current interactive mode, and once exited, it is invalidated. Therefore, we can set the PYTHONPATH environment variable according to the 2nd step of the search path rule can satisfy the different use situation can find the module.


Python Learning Notes-module Introduction (iii)-module package and search path

Related Article

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.