Explain the from in Python. Import Absolute Imports Statement

Source: Internet
Author: User
Relative or absolute Import
More complex parts have been implemented since python2.5: Importing a module can specify an import that uses absolute or package-relative. This plan will be moved to make the absolute import the default details in other versions of Python.
Let's say you have a package directory, like this:

pkg/pkg/__init__.pypkg/main.pypkg/string.py

The above defines a package called pkg containing Pkg.main and pkg.string two sub-modules. Consider the code in ' main.py ', what will happen if we execute the statement import string in python2.4 or earlier versions? He will query the directory of the package to perform a relative import, find pkg/string.py, import the contents of the file as a pkg.string module, the name of the boundary of this module is ' string ' in the namespace of the Pkg.main module.
If pkg.string is what you want, this is very nice. But what if you just want the basic string module of Python?
There is no clear way to ignore pkg.string and to look for basic modules; In general you have to look at the contents of Sys.modules, which is a little unclear. Holger Krekel's PY.STD package provides a neat way to perform the import method from the base library, Improt py;py.std.string.jion (), but that package is not available in the Python installation process.

Reading the code is also unclear in terms of relative imports, as readers may confuse the use of string and pkg.string modules. Python users can immediately know that it is a different name between the base library and their own package module, but you cannot protect your own submodule name in a new version of Python.

From python2.5, you can open import behavior directly to the absolute import using a from __future__ import absolute_import. This absolute import behavior will become a default detail in future Python. Once the absolute import is defaulted, the import string will always look for the base library. It is recommended that users use absolute imports as much as possible, so it is appropriate to use the From Pkg Improt string in your code. In the python2.* version, you need:

From __future__ import Absolute_import

Example 1:
For example: The code in the module A.B.C:

From. Import D     # Imports A.b.dfrom: Import E    # Imports A.efrom. F Import G    # Imports A.F.G,.. and F are attached, with no spaces in the middle

. Represents the current directory,.. Represents a previous level of directory, ... Represents the upper-level directory.

Example 2:

Directory structure:

package/__init__.py subpackage1/  __init__.py  modulex.py  moduley.py subpackage2/  __init__.py  modulez.py modulea.py

In subpackage1/modulex.py or subpackage1/__init__.py, you can import the module like this:

From. Moduley import spamfrom. Moduley import spam as hamfrom. Import Moduleyfrom. SubPackage1 import Moduleyfrom. Subpackage2.modulez import Eggsfrom. Modulea Import Foofrom ... package import barfrom ... sys import path

Note that the From ... sys import path is legal, but is not recommended. The direct from sys import path is OK.

  • 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.