[Python Basics] reference system (The Import System)

For tall buildings to avoid worry, worry also follow me on tall buildings. The line of several country changed, how many friends do bald.
To go off, to go off. Adults do not always Monkey? Source clouds no fixed yuan, it was like the clouds are free.

I learned Python from short work of them one thing, the more modular, the more you will find seemingly ordinary import python is a great mystery.

  1. Search path when the python python file references are often pwd terminal of the current. (Sys or may be at the os module append expected path)
  2. In python looks a bit like a reference file is executed the file again.
  3. Relative path python is really a lot of restrictions, in my opinion seems to be the set of her when I could use?

So, ah, I do not make people do not understand Python reference mechanism of it! JOJO!

From the Python Software Foundation learned of the reference system (ImportSystem):

When a importtime command is executed, the program performs a standard library __import__()functions. It is worth noting that if you are using importlib.import_module()can bypass this function uses its own reference mechanism.

When a module is first introduced, Python looks for the module, if found, will create a module object (module object). otherwiseraise ModuleNotFoundError

Package

Package concept in Python is structured to manage numerous modules. It is generally understood Package is a folder, rather Module is a separate file. But in fact only Module Package subclass, the only difference is a variable having a Module Package does not have__path__

When introduced from the sub-packages Package (subpackage) of the main package (parent package) is __init__.pyalso executed (you want to get a little matryoshka nesting dolls have large open concept of)

This is an example

parent/
    __init__.py
    one/
        __init__.py
    two/
        __init__.py
    three/
        __init__.py

If we execute import parent.onewhen we will first execute parent.__init__.pyand then execute one.__init__.py.

Namespace package

(Ah, see this namespace to a head two large)

Namespace is a python objects in the index system. Namespace package is mainly to solve the current program does not object module file system (a compressed package, there is a network location and other forms) situation. Currently not encountered this kind of application environment, it would not be understood in detail.

Find module

This is more important ┗ | `O '| ┛ wailing ~~

This package has been introduced Yo

All packets have been introduced in the sys.modulesreservoir so as to avoid repeating the introduction.

Look So far, my doubts have not answered. . . But it has been very sleepy, and by chance the series again?

Guess you like

Origin www.cnblogs.com/ziangzhang/p/11964991.html