Introduction to modules and packages in Python-Python tutorial

Source: Internet
Author: User
This article mainly introduces the modules and package concepts in Python. This article describes the Module Overview, Module namespace, import module, and import module attributes (from... Import ...) For more information, see Module Overview

If the module organizes Python code logically, the file is the method for organizing the module on the physical layer.
Therefore, ** a file is considered as an independent module, and a module can also be considered as a file. The module file name is the module
The Block name with the extension. py. Unlike other languages that can import classes, you can import module or module attributes in Python **.

Module namespace

A namespace is a ing between names and objects.

Import Module

Import Module)

Method 1

The code is as follows:


Import module1
Import module2
...
Import moduleN


Method 2

The code is as follows:


Import module1 [, module2 [,... moduleN]


Import module attributes (from... Import ...)

You can call from-import to import the name to the current namespace. This means that you do not need to use the attribute/period attribute identifier to access the module identifier. For example, you need to access the var name in the module to be imported as follows:

The code is as follows:


From module import name1 [, name2 [,... NameN]


The module or attribute is renamed after being imported.

The code is as follows:


Import......
From... import......


Package

A package is a hierarchical file directory structure. the module corresponds to a single file, and the package corresponds to a directory. in the same way as the class and module, the package also uses sentence points to identify their elements. Use the quasi-import and from-import statements to import modules in the 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.