Python Basics-Package

Source: Internet
Author: User

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

Whether it is an import or a from...import form, it is necessary to realize--this is a package--when you encounter a dot in an imported statement (not in use).

A package is a directory-level folder that is used to form a py file (the nature of the package is a directory containing the __init__.py file).

Import file, the resulting namespace name from the file, import package, the name of the resulting namespace is also derived from the file, that is, the __init__.py under the package, the import package is the essence of the import of the file.

In the Python3, even if there is no __init__.py file under the package, import package will still not error, and in Python2, the package must have the file, or import packet error.

[[Email protected] module]#Tree. ├──bonf#Package│?? ├──__pycache__│??│??└──test1.cpython-36.pyc#The cache file generated when importing│??└──test1.py Module └──conf#Package├──__pycache__│??├──test2.cpython-36. Pyc│??└──test1.cpython-36. pyc├──test2.py#Module└──test.py#Module4 Directories, 6Files#content in the package#!/usr/bin/env pythondefFunc1 ():#There is a func1 function in the module test1 of the bonf package.    Print("bonf")                                                               ~"bonf/test1.py"3L, 50C#another package conf#!/usr/bin/env pythondefFunc1 ():#test1 function func1 under CONF module under Package    Print("function 1")                                                            ~"conf/test1.py"3L, 53C

Now to import these two packages

Import Bonf.test1 import conf.test1  # no error

Now to refer to the functions in them

>>> bonf.test1.func1 () bonf>>> conf.test1.func1 ()  # No problem function 1

As we can see, modules with the same name under two packages do not conflict, they come from their respective namespaces.

You can also use From-import, but this overrides the namespace.

 from Import test1>>> test1.func1 () function 1fromimport  test1>>>  TEST1.FUNC1 () bonf

Multi-layered is also possible

Import bonf.donf.test10>>> donf.test10.func ()   # less than a layer, the path to Traceback (most Recent call last):  '<stdin>' in <module>  ' donf '  is  not defined>>> bonf.donf.test10.func () 2 floor

Relative Import and Absolute import

 from import test1  # Absolute fromimport test1    # relative

Python Basics-Package

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.