Python Learning Note III: Modules

Source: Internet
Author: User

One: module

A module is a py file, which defines some business functions. Reference modules, which can be imported using Import statements. After the module is imported, the function in the module is used by the module. Function name (parameter) . If there are multiple modules with the same name, the previous module name needs to be added to the package path.

If we only need to import several functions in the module, we can use the From Module name Import function name, function name,.. , so that you can use the function name in your code to invoke the function without a path.

To avoid conflicts with the same name, you can also set an alias at the same time as the import function, from themodule name, import function name as Alias , and then in code you can use the alias to invoke the function.

Two: Package

In order to avoid conflicts of the same name, the module is usually managed in the package and imported by the full path.

A package in Python is a special kind of folder that has a _init_.py file at each level, even if the contents of the file are empty. The purpose of this file is to identify the folder as a package.

To facilitate the invocation of the methods in the module below, aliases can also be used when importing the module: Import module as alias

Three: Dynamic Import module

Python Import module error will be reported Importerror exception, can be based on this feature, catch import exception to dynamically import. Such as:

Try :      from Import Stringio except importerror:      from Import Stringio

IV: Use the new version of the module in the old version

When a feature of a new version is incompatible with an older version, the feature will be added to the __future__ module in the old version so that the old code can test the new feature in the old version. So you can import the __future__ module in the old version of the Python program, and then call the new version of the function.

V: Use of third-party modules

Python official provides the pip install third-party module name. PY directive, you can find the name of the third-party module to use on the Https://pypi.python.org/pypi Central warehouse, and then use the command line to install it.

(similar to the practice of Maven import dependency packages in Java development)

Python Learning Note III: Modules

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.