Python __init__.py Action

Source: Internet
Author: User
Tags win32

The purpose of the __init__.py file is to change the folder into a Python module, with a __init__.py file in each of the modules in the Python package.

Normally the __init__.py file is empty, but we can add additional functionality to it. When we imported a package, we actually imported its __init__.py file. This allows us to bulk import the modules we need in the __init__.py file, instead of needing one import.

# package# __init__.pyimport reimport urllibimport sysimport os# a.pyimport package print(package.re, package.urllib, package.sys, package.os)

Note that the reference file in the __init__.py file is accessed here, and you need to add the package name.

There is also an important variable in __init__.py, __all__, which is used to import modules all.

# __init__.py__all__ = [‘os‘, ‘sys‘, ‘re‘, ‘urllib‘]# a.pyfrom package import *

The modules and packages registered in the __all__ list in the __init__.py file are imported into the current file.

It can be learned that the __init__.py main control package import behavior. To understand clearly the role of the __init__.py file, you need to know more about the import statement reference mechanism:

The objects that can be imported by the import statement are the following types:

    • Module files (. py files)
    • C or C + + extensions (compiled as shared libraries or DLL files)
    • Package (contains multiple modules)
    • Built-in modules (written using C and linked to the Python interpreter)

When the module is imported, the interpreter looks for the import file 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 list the first element of the empty string represents the current directory.

About the. pyc file with the. pyo file

The Assembly of the. py file is only performed when the import statement executes, and when the. py file is first imported, it is assembled into a byte code and the bytecode is written to a. pyc file of the same name. Each import operation then executes the. pyc file directly (when the modification time of the. py file is changed so that a new. pyc file is generated), and when the interpreter uses the-o option, the. pyo file with the same name is used, which removes the assertion (assert), the break number, and other debug information. Smaller size and faster to run. (using the-oo option, the generated. pyo file ignores the document information)

Import Module

The module is usually a separate. py file, which can be referenced directly with import and can be used as a module file type with. py,. Pyo,. PYc,. PYD,. So,. dll

When importing a module, the interpreter does the following:

    1. The name of the imported module creates a new namespace through which you can access the properties and methods of the imported module.

    2. Executes the source code file in the newly created namespace.

    3. Create an object called the source code file that references the namespace of the module so that the functions and variables in the module can be accessed through the object

The import statement can be used anywhere in the program, and 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 module names to module objects for all imported modules.

Importing packages

Multiple interrelated modules make up a package for maintenance and use, while allowing for limited avoidance of namespace collisions. In general, the structure of a package can be this:

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

There are several ways to import:

import subpackage1.a # 将模块subpackage.a导入全局命名空间,例如访问a中属性时用subpackage1.a.attrfrom subpackage1 import a # 将模块a导入全局命名空间,例如访问a中属性时用a.attr_afrom subpackage.a import attr_a # 将模块a的属性直接导入到命名空间中,例如访问a中属性时直接用attr_a

Using the FROM statement, you can import the module directly into the current namespace, and the from statement does not reference the namespace of the imported object, but instead imports the imported object directly into the current namespace.

Python __init__.py Action

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.