10. Python Module

Source: Internet
Author: User

Module

1, module definition : Used to logically organize Python code to achieve a specific function, essentially a. py end of the Python file.

2, the module import method :

Suppose there are 2 modules module_01 and module_02, with the following code:

1 #module_01 Module2Module_name ="module_01"3 defSayhi (call_name):4     Print("executed in module_01, called by%s"%call_name)5 deflogger ():6     Print("logger in the%s"% module_name)
1 #module_02 Module2Module_name ="module_02"3 defSayhi (call_name):4     Print("executed in module_02, called by%s"%call_name)5 deflogger ():6     Print("logger in the%s"% module_name)

1. Import module_name (Importing a single module module_name)

1 # Main module 2 Import # Import module_01 Module 3 " Main " 4 module_01.sayhi ("main"# execution results are executed in module_01, called by main 

2. Import module_name,module2_name (importing multiple modules Module_name,module2_name)

 #  main module  import  module_01,module_02 #   import module_01,module_02 module  module_name = "  main  "  module_01.sayhi (   " main   ) #   execution results are executed in module_01, called by main  Module_02.sayhi ( " main  " ) #   executed in module_02, called by main 

3. From module_name Import * (Import all code in module_name module, be cautious )

1 # Main module 2  from Import *3"main"4# execution results are executed in module_01, Called by main 5# Execution Result logger in the module_01

Note that this import is essentially a copy of all module_01 code, placed in the main module, so you do not need to add a module name when calling variables and functions in the Module_01 module. So why use caution, let's modify the code in the main module.

# Main module  from Import *"main"def  logger ():    Print ("logger in the%s" % module_name) logger ()

When the logger function is called, the result of execution should be logger in the main. This means that the Looger function in the Mian module is executed instead of the module_01. According to the above-mentioned from module_01 Import * This method is to put all the code in the module into the Mian module, so the code above the main module is equivalent to

1 #Main module2 #From module_01 Import *3 #--------------Code------------------for the module_01 module4Module_name ="module_01"5 defSayhi (call_name):6     Print("executed in module_01, called by%s"%call_name)7 #----------------Code--------------------for the Mian module8 deflogger ():9     Print("logger in the%s"%module_name)TenModule_name ="Main" One deflogger (): A     Print("logger in the%s"%module_name) -Logger ()

For the variable module_name, the 4th line of the code is assigned the value "Module_01", and then the 10th line of the code is assigned the value "main", so the value of Module_name is "main", because of the function name and variable name, So the logger function is the same. So the question is, if the code for the main function is this way:

# Main module " Main " def logger ():     Print ("loggerinthe%s" % module_name)  from Import *logger ()

The result of the execution will be God horse appearance?

4, from module_name import m1,m2 (Import module module_name m1,m2)

This kind of import saves, too simple I'm embarrassed to say

5, from MODULE_NAME import M1 as M1_test (Import module Module_name M1 and modify M1 name M1_test)

One problem with the previous 3rd import approach is that if the Looger function is also defined in the main module, it will be overwritten with the Looger in the Module_01 module, and the 5th import method is to solve the problem:

1 #Main module2Module_name ="Main"3  fromModule_01ImportLogger as Module_01_logger4 deflogger ():5     Print("logger in the%s"%module_name)6Logger ()#execution Result Logger in the main7Module_01_logger ()#execution Result Logger in the module_01

6. Import package_name (Importing package)

The meaning of a package in Python is essentially a directory with a __init__.py file, and after the package is imported, only the functions and variables in the package's __init__.py file can be called. For example:

1 # the __init__.py in package_01 2 def logger (): 3     Print ("loggerinthe package_01__init__.py")

The code in the main module is as follows:

# Main module Import  # Execution Result logger in the package_01__init__.py

At this point, we add a module package_module_01 in package_01, the code is as follows:

1 # package_01 bag under the package_module_01.py 2 def logger (): 3     Print ("loggerinthe package_module_01.py")

At this point, I want to call the logger method in Package_module_01 in the module, the code of the main module is as follows:

# Main module Import  # Execution result error: Attributeerror:module ' package_01 ' has no attribute ' package_module_01 '

Nani? The error, the tragedy! As we said before, for a package import, we can only invoke variables and functions in __init__.py, and if we need to call the variables and functions in package_module_01, we need to use __init__.py to make a bridge, we first in the __init Import package_module_01 in __.py. Using the statement: from. Import Package_module_01, this is when we execute the main module successfully.

3. Path Search

The above-mentioned modules and packages are imported based on each module in the same directory, then, how to handle the import of modules between different directories? In fact, we say that the module import is done in 2 steps, path search and module processing, below we see how to import modules of other directories.

Suppose there are 2 directories dir_01 and dir_02, where Dir_01 has a module module_01,module_01 a logger function, Dir_02 has a module module_02, our demand is in Module_ 02 in Import MODULE_01 calls its logger function.

# module_01 module in the dir_01 directory def logger ():     Print ("loggerinthe module_01")
 1  #   MODULE_02 module in dir_02 directory  2  import  Span style= "COLOR: #000000" > Sys,os  3  Dir_path = Os.path.dirname ( Os.path.dirname (Os.path.abspath (__file__   4  sys.path.append (dir_path)  5  from  dir_01  Import   module_01  6  module_01. Logger () #   execution result logger in the module_01  

Where Sys,os are system-standard modules, __file__ gets the relative path of the current file, Os.path.abspath () Gets the absolute path of the file, Os.path.dirname () Gets the parent directory of the current directory, Sys.path.append ( ) to add the path to the environment variable of the system. We then use the module import statement: From dir_01 import module_01 you can find the Module_01 module.

4, Module classification:

The modules are divided into three types:

    • Custom Modules (we've written all of our custom modules)
    • Built-in standard modules (also known as standard library Os,sys are built-in modules)
    • Open source module (some of the better modules written by Daniel)

10. Python Module

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.