A detailed explanation of the role of __init__.py files in Python

Source: Internet
Author: User
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, Pac Kage.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 import module.

2. Execute 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:


There are several ways to import:

Import subpackage1.a # Imports module Subpackage.a into the global namespace, for example, when accessing a property in a Subpackage1.a.attrfrom subpackage1 import a # Imports module A into the global namespace, For example, when accessing a property in a, use A.attr_afrom subpackage.a import attr_a # To import the properties of module a directly into the namespace, such as accessing a property in a directly using the FROM statement with the Attr_a to import the module directly into the current namespace. The FROM statement does not refer to the namespace of the imported object, but instead imports the imported object directly into the current namespace.

The above is a small part of the python to introduce the role of __init__.py file in detail, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!

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.