python\ Modules and Packages

Source: Internet
Author: User

1. Modules and Packages

A package is a way to organize the Python module namespace by using the module name

Whether it's in import or From...import form, it's the first time to be alert when you encounter a dot in an imported statement (rather than in use): It's about the package. Import syntax

The essence of a package is a directory containing __init__.py files

In the case of imports, the left side of the point must be a package.

from Back Import the imported module must be a definite one that cannot take a point, otherwise there will be a syntax error .

1.1__init__.py file

Either way, the first time the package is imported or any other part of the package, the __init__.py file under the package is executed sequentially (we can verify it by printing a line within each package's file), which can be empty, but it can also hold some code to initialize the package.

1.2 From GLANCE.API Import *

Here is to import all from the package API, in fact, the statement will only import the package API under the __init__.py file defined in the name, we can define the __all___ in this file:

#在__init__. PY definition x=10def func ():    print (' from api.__init.py ') __all__=[' x ', ' func ', ' policy ']

At this point we are in the glance of the files in the same class to execute from GLANCE.API import * To import the contents of the __all__ (versions still cannot import).

1.3 Absolute Import and relative import

Our top package glance is for others to use, and then within the glance package will also have each other to import each other needs, this time there are absolute import and relative import two ways:

Absolute import: Starting with glance

Relative import: With. Or. (can only be used in one package and not in different directories)

In particular, it is important to note that you can use import import built-in or third-party modules, but absolutely avoid using import to import a child module of a custom package, you should use the From ... import ... of absolute or relative import, and the relative import of the package can only be used from the form.

python\ Modules and 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.