S 0en s 1en__.py documents

  • 2020-05-12 02:48:50
  • OfStack

S function is to turn the folder into one Python module. S 6en files are available in the packages of each module in Python.

S: usually s s 10en__.py file is empty, but we can add other functions to it. When we import a package, we are actually importing its s 12en__.py file. In this way, we can import the modules we need in batches with s 14encancer.py instead of importing one module at a time.


# package
# __init__.py
import re
import urllib
import sys
import os
# a.py
import package 
print(package.re, package.urllib, package.sys, package.os)

Note here to access the reference file in the s 19encancer.py file, you need to add the package name.

S 25en with another important variable, s 26en__, which is used to import the whole module.


# __init__.py
__all__ = ['os', 'sys', 're', 'urllib']
# a.py
from package import *

At this time the modules and packages registered on the s 30en__.py file with s 32en__ list will be imported into the current file.

It is understood that s 37en mainly controls the import behavior of the packages. To understand the function of the s38en__.py file, you need to know more about the import clause reference mechanism.

Objects that can be imported by import statements are of the following types:

The & # 8226; Module file (.py file)

The & # 8226; C or C++ extension (compiled as a Shared library or DLL file)

The & # 8226; Package (contains multiple modules)

The & # 8226; Built-in module (written in C and linked to Python interpreter)

When importing a module, the interpreter looks up the imported files in the order of the directories in the sys.path list.


import sys
>>> print(sys.path)
# Linux:
['', '/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-sunos5',
'/usr/local/lib/python3.4/lib-tk',
'/usr/local/lib/python3.4/lib-dynload',
'/usr/local/lib/python3.4/site-packages']
# Windows:
['', 'C:\\WINDOWS\\system32\\python34.zip', 'C:\\Documents and Settings\\weizhong', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34\\lib\\plat-win', 'C:\\Python34\\lib\\lib-tk', 'C:\\Python34\\Lib\\site-packages\\pythonwin', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages', 'C:\\Python34\\lib\\site-packages\\win32', 'C:\\Python34\\lib\\site-packages\\win32\\lib', 'C:\\Python34\\lib\\site-packages\\wx-2.6-msw-unicode']

Where the first element of list, an empty string, represents the current directory.

About.pyc documents and.pyo documents

The.py file is compiled only when the import statement is executed. When the.py file is imported for the first time, it is compiled into bytecode and written to the.pyc file of the same name. The.pyc file (which generates a new.pyc file when the.py file is modified at a different time) will be used when the interpreter USES the -O option, which removes assertions (assert), line breaks, and other debugging information, making it smaller and faster. (using the -OO option, the generated.pyo file ignores the documentation information.)

The import module

Modules are usually separate.py files that can be directly referenced using import. The file types that can be used as modules are.py,.pyo,.pyc,.pyd,.so,.dll

When importing a module, the interpreter does the following:

1. Create a new namespace for the name of the imported module, through which you can access the properties and methods of the imported module.

2. Execute the source file in the newly created namespace.

3. Create an object called a source code file that references the module's namespace so that you can access functions and variables in the module

The import statement can be used anywhere in the program. You can import the same module multiple times in the program, but the code in the module is only executed when the module is first imported. The following import statement simply creates a reference to the module namespace.

The sys.modules dictionary holds the mapping of all the imported module names to the module objects.

Import packages

Multiple associated modules make up a package that is easy to maintain and use, while limiting namespace conflicts. 1 generally, the package structure can look like this:


package
|- subpackage1
|- __init__.py
|- a.py
|- subpackage2
|- __init__.py
|- b.py

There are several ways to import:


import subpackage1.a #  The module subpackage.a Import the global namespace, such as access a Is used for properties in subpackage1.a.attr
from subpackage1 import a # The module a Import the global namespace, such as access a Is used for properties in a.attr_a
from subpackage.a import attr_a #  The module a Is directly imported into the namespace, such as access a Is used directly when the attr_a 
 use from Statement can import the module directly into the current namespace, from The statement does not refer to the namespace of the imported object. Instead, the imported object is introduced directly into the current namespace. 

Related articles: