The functions of the _ init _. py file in Python

Source: Internet
Author: User
_ Init __. the role of the py file is to change the folder into a Python module. in the package of each module in Python, there are _ init __. py file. this article mainly introduces _ init _ in Python __. the role of the py file is explained in detail, which is very good and has reference value. if you need it, you can refer to _ init __. the role of the py file is to change the folder into a Python module. in the package of each module in Python, there are _ init __. py file.

The _ init _. py file is usually empty, but other functions can be added for it. When importing a package, we actually imported its _ init _. py file. In this way, we can import the required modules in batches in the _ init _. py file, instead of importing them one by one.

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

Note that you need to add the package name to access the reference file in the _ init _. py file.

_ Init _. py also has an important variable __all __, which is used to import all modules.

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

In this case, the modules and packages registered in the _ init _. py file _ all _ list are imported into the current file.

We can understand that __init _. py mainly controls the import behavior of packages. To clearly understand the functions of the _ init _. py file, you need to learn more about the import statement reference mechanism:

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

• Module file (. py file)

• C or C ++ extensions (compiled as shared library or DLL files)

• Package (including multiple modules)

• Built-in modules (written in C and linked to the Python interpreter)

When importing the module, the interpreter searches for the Import file in the directory order 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 first empty string of the list element represents the current directory.

About the. pyc file and. pyo file

. The py file assembly is only executed when the import statement is executed. when a py file is imported for the first time, it is compiled into bytecode and written to the same name. in the pyc file. Later, each import operation will be executed directly. pyc File (when. the modification time of the py file is changed, and a new one is generated. pyc file), when the interpreter uses the-O option, the same name will be used. pyo file, which removes assert, line breaks, and other debugging information. the file is smaller and runs faster. (If the-OO option is used, the generated. pyo file ignores the document information)

Import Module

The module is usually a separate. py file, which can be directly referenced by import. the file types of the module can be. py,. pyo,. pyc,. pyd,. so,. dll.

When importing a module, the interpreter does the following:

1. create a new namespace with the name of the imported module. with this namespace, you can access the attributes and methods of the imported module.

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

3. create an object named the source code file. this object references the namespace of the module, so that you can access functions and variables in the module through this object.

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 imported for the first time. The following import statement simply creates a reference to the module namespace.

The sys. modules dictionary stores the ing between the module names of all imported modules and the module objects.

Import package

Multiple associated modules form a package to facilitate maintenance and use, while avoiding namespace conflicts. Generally, the package structure can be as follows:

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

There are several import methods:

Import subpackage1.a # subpackage the module. a imports the global namespace. for example, subpackage1.a is used to access the attributes in. attrfrom subpackage1 import a # import Module a to a global namespace. attr_afrom subpackage. a import attr_a # import the attributes of module a to the namespace directly. for example, when accessing the attributes of module a, you can use the from statement to directly import the module to the current namespace, the from statement does not reference the namespace of the imported object, but directly introduces the imported object to the current namespace.

The above section describes _ init _ in Python __. the role of the py file is explained in detail and I hope it will be helpful to you. if you have any questions, please leave a message for me and I will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!

For more details about the functions of the _ init _. py file in Python, refer to the PHP Chinese website!

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.