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.