Python learning (module)

xiaoxiao2021-03-06  63

Module Search Path: Import a name

When the spam module, the interpreter first searches in the current directory.

Spam.py file, then in environment variables

PythonPath refers to the directory list in the directory list, then the environment variable

Path list in PATH. in case

PythonPath is not set, or the file is not found, then search for the installation directory,

In Unix, usually

.: / usr / local / lib / python. In fact, the interpreter is

The path directory search module specified by the sys.path variable, which is initialized when the variable contains the input script (or the current directory),

PathPath and installation directory.

The package is usually used with a structured module with a "dot module name" namespace. For example, named

A.B's module is called "

A "of the package" "

B "Submunition. Just as the module to save different module architectures can avoid mutual conflicts between global variables, using polka dot module name storage

NUNPY or

Different class libraries such as Python Imaging Library can avoid naming conflicts between modules.

When the module is imported, Python searches the subdirectory of the storage package via the directory list in sys.path.

There must be a __init__.py file existence to make Python as a package as the directory; this is to prevent some directories from using the general name such as "String" without intending to overwrite in the subsequent module search path. The correct module. In the simplest case, __ init__.py can be just an empty file, but it may also contain the initialization code of the package, or set the __all__tric.

The package users can import legitimate modules from the package, for example:

Import snound.effects.echo

This is imported into the Sound.effects.echo submodule. It must be referenced by a complete name.

Sound.effects.echo.echofilter (Input, Output, Delay = 0.7, Atten = 4)

There is a way you select when importing the package:

From Sound.effects Import Echo

This loads the ECHO sub-module and allows it to be used without a packet prefix, so it can call as follows:

Echo.ecHofilter (Input, Output, Delay = 0.7, Atten = 4)

There is also another variant for direct import functions or variables:

From Sound.effects.echo Import Echofilter

This will then load the Echo submodule again, but this can directly call its echofilter () function:

Echofilter (Input, Output, Delay = 0.7, Atten = 4)

It should be noted that when importing the package using the from package import item, this child (item) can be a submodule (or a sub-package) in the package, or other named function, a function, class defined in the package. Or variables. The import statement first checks if there is this child in the package, if not, it assumes this is a module and try loading it. If it does not find it, it will trigger an ImportError exception.

Instead, when using syntax like Import Item.subitem.SubSubItem, these subtries must be packets, and the final child can be package or module, but cannot be class, functions, or variables defined in the previous subkey.

转载请注明原文地址:https://www.9cbs.com/read-112158.html

New Post(0)