__init__.py file function analysis in Python module package

Source: Internet
Author: User
This example describes the __init__.py file feature in the Python module package. Share to everyone for your reference, as follows:

It has been more than a year since the development of Django, but there is little attention to the meaning of the existence of the __init__.py file in the Python module, and the introduction of it is a big surprise.

Module Package:

A package is usually always a directory, and a file headed by the directory is __init__.py. Then there are some module files and subdirectories, if there are __init__.py in the subdirectory then it is the child package of this package.

It's almost like this:

package1/__init__.py module1.py module2.py Package2
package1/package2/__init__.py module1.py module2.py

We can import a package like this:

Or, a sub-module and a sub-package are transferred:

From Package1 import Module1 from Package1 import Package2        , import packag1.module1        

You can drill down into several layers of package structure:

From Package1.package2 import Module1            

In general, I think, the essence of the package should be in the __init__.py this file.

More interested in Python related content readers can view this site topic: "Python Picture Operation skills Summary", "Python data structure and algorithm tutorial", "Python Socket Programming Skills Summary", "Python function Use Tips", " Python string manipulation Tips Summary, Python Introductory and Advanced classic tutorials, and Python file and directory operations tips

I hope this article is helpful for Python program design.

  • 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.