[Python] Importing Python Modules

Source: Internet
Author: User

Introduction

TheImportAndFrom-importStatements are a constant cause of serious confusion for newcomers to Python. Luckily, once you 've figured out what they really do, you'll never have problems with them again.

This note tries to sort out some of the more common issues relatedImportAndFrom-importAnd everything.

There are always Ways to Import a Module

Python provides at least three different ways to import modules. You can useImportStatement,FromStatement, or the builtin_ Import __Function. (There are more contrived ways to do this too, but that's outside the scope for this small note .)

Anyway, here's how these statements and functions work:

  • Import XImports the module X, and creates a reference to that module in the current namespace. Or in other words, after you 've run this statement, you can useX. nameTo refer to things defined in module X.

  • From X import *Imports the module X, and creates references in the current namespace to allPublicObjects defined by that module (that is, everything that doesn't have a name starting with "_"). or in other words, after you 've run this statement, you can simply use a plainNameTo refer to things defined in module X. But X itself is not defined, soX. nameDoesn' t work. And ifNameWas already defined, it is replaced by the new version. And ifNameIn X is changed to point to some other object, your module won't notice.

  • From X import a, B, cImports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now useAAndBAndCIn your program.

  • Finally,X = _ import _ ('x ')Works likeImport X, With the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.

    What Does Python Do to Import a Module?

    When Python imports a module, it first checks the module registry (Sys. modules) To see if the module is already imported. If that's the case, Python uses the existing module object as is.

    Otherwise, Python does something like this:

    1. Create a new, empty module object (this is essential a dictionary)
    2. Insert that module object inSys. modulesDictionary
    3. Load the module code object (if necessary, compile the module first)
    4. Execute the module code object in the new module's namespace. All variables assigned by the code will be available via the module object.

      This means that it's fairly cheap to import an already imported module; Python just has to look at the module name up in a dictionary.

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.