Functions of the _ init _. py file in the python module package

Source: Internet
Author: User

When using pydev to develop Python scripts in eclipse, I encountered this phenomenon. When I create a pydev package, an empty _ init _ is automatically generated __. py file, because it is a beginner in Python, so I do not know what the role of this empty file is, because there is nothing to write here, So I deleted this file directly, as a result, my package icon is automatically changed to a folder icon. What is the problem!

 

Originally, every package in the python module had a _ init __. the py file (which defines the attributes and methods of the package) is followed by some module files and subdirectories. If the subdirectory also contains _ init __. PY, It is the sub-package of this package. When you import a package as a module (for exampleXMLImportDom), Actually imported its_ Init _. pyFile.

A package contains a special file._ Init _. pyDirectory._ Init _. pyThe file defines the attributes and methods of the package. In fact, it can not be defined; it can be just an empty file, but it must exist. If_ Init _. pyThis directory is just a directory, not a package. It cannot be imported or contain other modules and nested packages.

 

 

_ Init _. py file:

_ Init _. py controls the package import behavior. If _ init _. py is empty, nothing can be done to import the package only.

>>> import Package1
>>> Package1.Module1
Traceback (most recent call last):
  File "D:/Work Space/Python practice/MyPractice/src/test.py", line 8, in <module>
    aa=Package1.Module1
AttributeError: 'module' object has no attribute 'Module1'

We need to pre-import module1 in _ init _. py:

# File _ init _. py
Import module1

Test:

>>> import Package1
>>> aa=Package1.Module1
>>> print aa

Another important variable in _ init _. py is called _ all __. We sometimes make a trick to "import all", that is:

from PackageName import *

In this case, import the sub-modules and sub-packages registered in the _ init _. py file _ all _ list to the current scope. For example:

# File _ init _. py
_ All _ = ["module1", "module2", "subpackage1", "subpackage2"]

Test:

>>> from Package1 import *

>>>

 

Test11111111111111111111
Test222222

The _ init _. py file will be executed during import.

 

 

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.