The role of __init__.py in Python packages

Source: Internet
Author: User

1. __init__.py defines the properties and methods of the package

Generally empty file, but must exist, no __init__.py indicates that his directory is not a package

2. When importing the package, use

For example, there is a test directory with xx1.py,xx2.py,__init__.py three files under test

| Test

| | __init__.py

| | xx1.py

| | xx2.py

The test can be imported as a package, importing all the files under test

From Test import *

The actual import is the variable __all__ in __init__.py, i.e. __all__ = [' xx1 ', ' xx2 ']

3. __init__.py Add Library

For example, in __init__.py, edit the following: 

1 Import Re 2 Import OS

Then __all__ = [' re ', ' OS ']

The role of __init__.py in Python packages

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.