Python import module two ways and import path problem

Two ways to import modules:

  1. import + module name
    This method imports the module, when calling the function inside, use the module name.
    If the function is imported in the package, use the import package.
    When using the module, use the package. Module. Content
  2. from module name import function
    This method is to accurately import the functions in the module, and directly use the function name to call when calling, no need to write the module name
    from module name import *
    import all records in the __all__ variable parameter in the module Class and function
    If you import the module in the package with the from package import *
    , you can use the module directly.
    Note: When importing the module in the package in the form of from, make sure that there is an __init__.py file in the package and the _ _all__ includes the modules you imported******************************************* ******************************************
    Path problem of imported module: the following figure It is the search order of the python parser for the module location (the "2" is the path specified by the system environment variable) The
    Insert picture description here
    python parser's search order for the module location,
    so if you want to find a module under another path, we can put the path in " 4", that is, in the sys.path in the sys module, before the module code required by import, insert it at the top of sys.path (using the insert method). The path where the module is needed is enough.

Link: https://www.jianshu.com/p/6375dc25d976
Source: Jianshu

Guess you like

Origin blog.csdn.net/zhuyin6553/article/details/90319978