First, the basis of the module
1. Module
Self-contained, organized code snippets are modules
A module is the Pyhon highest-level program organizational unit that encapsulates program code and data for reuse. In practical terms, modules often correspond to Python program files.
Each file is a module, and the module can use the variable name defined by the import module after it has been imported into another module. The module can be processed by two statements and an important built-in function.
Import: Causes the Client (importer) to obtain a module as a whole.
From: Allows the client to obtain a specific variable name from a module file.
Reload: Provides a way to reload the module file code without aborting the Python program.
Reference: http://ipseek.blog.51cto.com/1041109/795782
2. Name space
A namespace is a insinuate collection of relationships from a name to an object.
Each module defines its own unique namespace, such as:
Import S1,s2#s1.py,s2.pys1.foo () S2.bar () S1.bar () #由于每个模块都有唯一的名称空间, and does not conflict even if the property is the same
The procedure for adding a name to a namespace involves binding an identifier to the specified object (as well as a reference count of the object plus 1).
3. Search path and path search
The import of a module requires a process called "Path search".
Search path: Find a group of directories
Path Search: Find an action for a file
Importerror:no module named MyModule
The error is that the module is not in the search path , causing the path search to fail!
When importing a module, the suffix name of the module is not taken, such as. py
Path to the Python search module:
1), the main directory of the program
2), Ptyhonpath directory (if already set)
3), Standard Connection Library directory (generally in/usr/local/lib/python2. x/)
4), the contents of any. pth file (if any). New feature that allows users to add an effective directory to the module search path
The. PTH suffix text file lists the directories in a row of rows.
These four combinations become sys.path,
>>> Import sys>>> Sys.path When importing, Python will automatically search from left to right for each directory in this list.
Second, module import
1. Import statement
Recommended:
Import is placed in front of the program and is sorted from top to bottom in the order of the Python standard library module, the Python third-party module, and the custom module.
2. From-import statement
You can import a specified attribute in a module: From module import name1[,nam2[,... Namen]]
Attention:
(1), import can use \ for line wrapping,
Example: from module import nam1,name2,\
Name3,name4 ....
(2), not recommended from module import *
(3), replace the name with as:
From module import name1 as Name2
3. Features of module import
(1), loading the execution module, that is, the import module's top-level code will be executed, usually including the module's global variables , classes and functions of the declaration . we encapsulate the code as much as possible in the function, which is a good module programming habit.
(2), a module can be imported (import) multiple times, but only load (load) once.
4. Module built-in function
(1), __import__ (): Functions for importing modules
The import statement actually calls the __import__ () function
If we have a testa.py file, we can use module=__import__ (' Testa ') to import it. This can be used with the module. property to use properties in testa.py
See other books
Three, the package
1, the concept of the package
A package is a hierarchical file directory structure that consists of modules and sub-packages.
2. Parsing package mechanism:
Each of the. py files is called a module,module that can be imported into each other
Module can be defined inside the package. Python defines the package in a slightly odd way, assuming that we have a parent folder that has a child subfolder. There is a module a.py. How do I get Python to know this file hierarchy? It's simple, each directory has a name of _init
_.py file. The contents of the file can be empty. This hierarchy is as follows:
Reference: http://huangliangfeixu.blog.163.com/blog/static/18974706220112741040442/
3. The role of __init__.py files in Python
Python has a __init__.py file in each of the module's packages, and with this file we can import the module in this directory.
So, what other functions does __init__.py have?
In fact, __init__.py inside can still have content, we import a package, actually imported its __init__.py file.
Click Continue to read:
4. Import
Refer to the picture above
Import web.web_catchimport web.child_web.demofrom web.child_web Import Demo
Python starter notes (module)