Concept of module (modue)
In the development of computer programs, as the program code more and more, in a file code will be more and more long, more and more difficult to maintain.
In order to write maintainable code, we grouped many functions into separate files so that each file contained relatively few code, and many programming languages used this way of organizing code. In Python, a. py file is called a module.
What are the benefits of using modules?
The greatest benefit is that the maintainability of the code is greatly improved.
Second, writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.
Therefore, there are three types of modules:
- Python Standard library
- Third-party modules
- Application Custom Modules
In addition, the use of modules can also avoid conflicting function names and variable names. Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name.
Module Import method1 Import Statement
| 1 |
importmodule1[, module2[,... moduleN] |
When we use the import statement, how does the Python interpreter find the corresponding file? The answer is that the interpreter has its own search path, which exists in the Sys.path.
So if, like me, there are files in the current directory with the same name as the module to be introduced, the module to be introduced will be screened out.
When you import foo, you will first create a new namespace to hold all the names defined in the foo.py, and then execute the foo.py within that namespace.
Some code, the last thing you need to know is the Import keyword
is to define a name, but at the moment we use import to define a module name Foo, which refers to the name
To foo.py's namespace, and Foo. is the way to find names from that namespace, you can use foo.__dict__ to see them.
When executing the source file (foo.py) code in the new namespace, all references or modifications to the global namespace are based on foo.py, not the current file (test.py) global namespace
Note 1:
Modules can be imported multiple times at the current location, but only the first import executes the code in the source file, because the first import will include the inside of the module
Loaded into memory, and subsequent imports at the current file location are pointing to the existing modules in memory
NOTE 2:
| 123 |
import logging as loglog.critical("www") |
2 from...import Statements
| 1 |
from modname importname1[, name2[, ... nameN]] |
This declaration does not import the entire ModuleName module into the current namespace, but only introduces the name1 or name2 individual inside it into the global symbol table of the module that executes the declaration.
Where the from...import* statement provides an easy way to import all the items in a module. However, such statements should not be used too much. In most cases, Python programmers do not use this approach, because the naming of other sources introduced may well overwrite existing definitions.
Packages (Package)
What if a different person writes the same module name? To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.
For example, a abc.py file is a named abc module, and a xyz.py file is a named xyz module.
Now, assuming that our abc and xyz these two module names conflict with the other modules, we can organize the modules through the package to avoid conflicts. Method is to select a top-level package name:
Once the package is introduced, all modules will not conflict with others as long as the package name in the top layer does not conflict with others. Now, the name of the view.py module becomes HELLO_DJANGO.APP01 .views , similarly, the Manage .py module name is Hello_django.manage.
Please note that each package directory will have a __init__.py file that must exist, otherwise Python will treat the directory as a normal directory (folder) instead of a package. __init__.pyit can be an empty file, or it can have Python code, because __init__.py it is a module, and its module name is the name of the corresponding package.
The calling package is the __init__.py file under the Execute Package
Note Point (important)
1--------------
In the Nod1 import Hello is not found, there are students said can find Ah, that is because your pycharm for you to add MyApp this layer of path into the sys.path inside, so you can find, however, once the program runs at the command line, the error. A classmate asked what to do? Simple Ah, you add this path to not just OK:
| 12345 |
import sys,osBASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.path.append(BASE_DIR)import hellohello.hello1() |
2--------------
| 12 |
if__name__==‘__main__‘: print(‘ok‘) |
"Make a. py both importable and executable"
If we are directly executing a. py file, the file then "__name__ = = ' __main__ '" is true, but if we import the file through import from another. py file, then __name__ The value is the name of our py file, not the __main__.
This feature also has a use: When debugging code, in "if __name__ = = ' __main__ '" to add some of our debugging code, we can let the external module calls do not execute our debugging code, but if we want to troubleshoot the problem, directly execute the module file, Debug your code to work! S
3
# #-------------Cal.pydef Add (x, y): return x+y##-------------main.pyimport cal #from module import cal def main (): cal.add # #--------------bin.pyfrom Module Import main Main.main ()
Python (17)