The function of __init__.py file in Python is detailed _python

Source: Internet
Author: User
Tags win32 in python

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

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

# 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 __init__.py file, you need to add the package name.

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

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

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

As you can see, __init__.py the main control package import behavior. For a clear understanding of the role of the __init__.py file, you need to learn more about the import statement referral mechanism:

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

• Module file (. py file)

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 you import a module, the interpreter finds the import file according to 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 ']

The empty string representing the first element of the list represents the current directory.

About. pyc files and. pyo files

The. py file is compiled only when the import statement is executed, and when the. py file is imported for the first time, it is compiled as a byte code and the bytecode is written to the. pyc file with the same name. Each import operation is then executed directly. The PYc file (when the. py file is modified 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 debugging information. Smaller in size and faster to run. (using the-oo option, the generated. pyo file ignores the document information)

Import Module

Modules are usually separate. py files that can be referenced directly by import and can be used as a file type for a module. PY,. Pyo,. PYc,. PYD,. So,. dll

When you import 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. Execute the source code file in the newly created namespace.

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

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

The Sys.modules dictionary stores the mappings of module names to module objects for all imported modules.

Import Package

Multiple associated modules make up a package that is easy to maintain and use while limiting the ability to avoid namespace conflicts. In general, the structure of a package can be as follows:

Package
| SubPackage1
| __init__.py
| a.py
| subpackage2
| __init__.py
|-b.py

There are several ways to import this:

Import subpackage1.a # Imports module Subpackage.a into global namespaces, such as subpackage1.a.attr from SubPackage1 import a when accessing properties in a
# Import module A into the global namespace, such as using the a.attr_a from
subpackage.a import attr_a # To import the properties of module a directly into the namespace, such as accessing the properties in a, for example, directly using the Attr_a 
Use the FROM statement to import a module directly into the current namespace, the from statement does not refer to the namespace of the imported object, but rather the imported object is introduced directly into the current namespace.

The above is a small set of Python to introduce the role of __init__.py file in detail, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.