Python3 module
In the previous chapters, we used the Python interpreter to program our scripts. If you exit and then enter the Python interpreter, all the methods and variables you defined will disappear.
For this purpose, Python provides a way to store these definitions in a file for use by some scripts or interactive interpreter instances. This file is called a module.
A module is a file containing all the functions and variables you define, and its suffix is .py. Modules can be imported by other programs to use functions and other functions in the module. This is also done using the python standard library.
The following is an example using modules in the python standard library.
#!/usr/bin/python3 # 文件名: using_sys.py import sys print('命令行参数如下:') for i in sys.argv: print(i) print('\n\nPython 路径为:', sys.path, '\n')
The execution results are as follows:
$ python using_sys.py 参数1 参数2 命令行参数如下: using_sys.py 参数1 参数2 Python 路径为: ['/root', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
1. import sys introduces the sys.py module in the python standard library; this is a method of introducing a certain module.
2. sys.argv is a list containing command line parameters.
3. sys.path contains a list of paths where the Python interpreter automatically finds the required modules.
import statement
If you want to use a Python source file, just execute the import statement in another source file. The syntax is as follows:
import module1[, module2[,... moduleN]
When the interpreter encounters an import statement, the module will be imported if it is in the current search path.
The search path is a list of all directories that the interpreter will search first. If you want to import the support module, you need to put the command at the top of the script:
support.py The file code is:
#!/usr/bin/python3 # Filename: support.py def print_func( par ): print ("Hello : ", par) return
test.py Introduce the support module:
#!/usr/bin/python3 # Filename: test.py # 导入模块 import support # 现在可以调用模块里包含的函数了 support.print_func("php")
The above example output results:
$ python3 test.py Hello : php
A module will only be imported once, no matter how many times you execute the import. This prevents imported modules from being executed over and over again.
When we use the import statement, how does the Python interpreter find the corresponding file?
This involves the Python search path. The search path is composed of a series of directory names. The Python interpreter looks for the introduced modules from these directories in turn.
This looks a lot like environment variables. In fact, the search path can also be determined by defining environment variables.
The search path is determined when Python is compiled or installed, and it should also be modified when installing new libraries. The search path is stored in the path variable in the sys module. To do a simple experiment, in the interactive interpreter, enter the following code:
>>> import sys >>> sys.path ['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages'] >>>
sys.path The output is a list, where the first item is empty The string '' represents the current directory (if it is printed from a script, you can see more clearly which directory it is), that is, the directory where we execute the python interpreter (for scripts, it is the directory where the running script is located).
So if, like me, there is a file with the same name as the module to be imported in the current directory, the module to be imported will be blocked.
After understanding the concept of search path, you can modify sys.path in the script to introduce some modules that are not in the search path.
Now, create a fibo.py file in the current directory of the interpreter or a directory in sys.path, the code is as follows:
# 斐波那契(fibonacci)数列模块 def fib(n): # 定义到 n 的斐波那契数列 a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b print() def fib2(n): # 返回到 n 的斐波那契数列 result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result
Then enter the Python interpreter and use the following command to import this module:
>>> import fibo
This does not write the function name directly defined in fibo into the current symbol table, but only adds the module fibo The name is written there.
You can use the module name to access functions:
>>> fibo.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> fibo.__name__ 'fibo'
If you plan to use a function frequently, you can assign it a local name:
>>> fib = fibo.fib >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
from...import statement
Python's from statement allows you to import a specified part from the module into the current namespace. The syntax is as follows:
from modname import name1[, name2[, ... nameN]]
For example, to import the module fibo fib function, use the following statement:
from fibo import fib
This statement will not import the entire fib module into the current namespace, it will only introduce a single fibonacci in fib into the global symbol table of the module that executes this statement. .
From...import* statement
It is also feasible to import all the contents of a module into the current namespace, just use the following statement:
from modname import *
This provides an easy way to import all projects in a module. However, this statement should not be overused.
In-depth module
In addition to method definitions, modules can also include executable code. These codes are generally used to initialize this module. This code will only be executed the first time it is imported.
Each module has its own independent symbol table, which is used as a global symbol table for all functions within the module.
So, the module author can safely use these global variables inside the module without worrying about messing with other users' global variables.
On the other hand, when you really know what you are doing, you can also access functions within the module through notation such as modname.itemname.
Modules can import other modules. Using import at the beginning of a module (or script, or elsewhere) to import a module is, of course, just a convention, not a requirement. The name of the imported module will be placed in the symbol table of the currently operating module.
There is also an import method. You can use import to directly import the names (of functions and variables) in the module into the current operation module. For example:
>>> from fibo import fib, fib2 >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This import method will not put the name of the imported module in the current character table (so in this example, the name fibo is not defined).
There is another way to import all (functions, variables) names in the module into the character table of the current module at once:
>>> from fibo import * >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This will import all the names Imported, except names starting with a single underscore (_). In most cases, Python programmers do not use this method, because the introduction of names from other sources is likely to overwrite existing definitions.
__name__ attribute
When a module is introduced for the first time by another program, its main program will run. If we want a certain program block in the module not to be executed when the module is imported, we can use the __name__ attribute to make the program block only execute when the module itself is run.
#!/usr/bin/python3 # Filename: using_name.py if __name__ == '__main__': print('程序自身在运行') else: print('我来自另一模块')
The running output is as follows:
$ python using_name.py
The program itself is running
$ python >>> import using_name 我来自另一模块 >>>
Note: Each module has a __name__ attribute. When its value is '__main__', it indicates that the module itself is running. Otherwise it is imported.
dir() function
内置的函数 dir() 可以找到模块内定义的所有名称。以一个字符串列表的形式返回: </p> <pre> >>> import fibo, sys >>> dir(fibo) ['__name__', 'fib', 'fib2'] >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']
If no parameters are given, the dir() function will list all the names currently defined:
>>> a = [1, 2, 3, 4, 5] >>> import fibo >>> fib = fibo.fib >>> dir() # 得到一个当前模块中定义的属性列表 ['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys'] >>> a = 5 # 建立一个新的变量 'a' >>> dir() ['__builtins__', '__doc__', '__name__', 'a', 'sys'] >>> >>> del a # 删除变量名a >>> >>> dir() ['__builtins__', '__doc__', '__name__', 'sys'] >>>
Standard module
Python itself comes with some standard module libraries, which will be introduced in the Python library reference document (that is, the "Library Reference Document" later).
Some modules are built directly into the parser. Although these are not built-in functions of some languages, they can be used very efficiently, and even system-level calls are no problem.
These components will be configured in different forms according to different operating systems. For example, the winreg module will only be provided to Windows systems.
It should be noted that there is a special module sys, which is built into every Python parser. The variables sys.ps1 and sys.ps2 define the strings corresponding to the primary prompt and secondary prompt:
>>> import sys >>> sys.ps1 '>>> ' >>> sys.ps2 '... ' >>> sys.ps1 = 'C> ' C> print('Yuck!') Yuck! C>
Package
Package is a way to manage the Python module namespace Form, taking "dot module name".
For example, the name of a module is A.B, then it represents a submodule B in package A.
Just like when using modules, you don’t have to worry about the mutual influence of global variables between different modules. Using the dot module name format, you don’t have to worry about the duplication of module names between different libraries.
In this way, different authors can provide NumPy modules or Python graphics libraries.
Suppose you want to design a set of modules (or call it a "package") that handles sound files and data in a unified manner.
There are many different audio file formats (basically distinguished by suffix names, such as: .wav, :file:.aiff, :file:.au,), so you need to have a set of constant Added module for converting between different formats.
And for this audio data, there are many different operations (such as mixing, adding echo, adding equalizer functions, creating artificial stereo effects), so you need a set of modules that you can never finish. to handle these operations.
Here is a possible package structure (in a hierarchical file system):
sound/ 顶层包 __init__.py 初始化 sound 包 formats/ 文件格式转换子包 __init__.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... effects/ 声音效果子包 __init__.py echo.py surround.py reverse.py ... filters/ filters 子包 __init__.py equalizer.py vocoder.py karaoke.py ...
When importing a package, Python will based on the directory in sys.path Look for the subdirectories contained in this package.
A directory will only be recognized as a package if it contains a file called __init__.py. This is mainly to avoid some clichéd names (such as string) from accidentally affecting the valid modules in the search path. .
In the simplest case, just put an empty :file:__init__.py. Of course, this file can also contain some initialization code or assign values to the __all__ variable (to be introduced later).
Users can only import specific modules in one package at a time, for example:
import sound.effects.echo
This will import the submodule: mod:song.effects.echo. He must use his full name to access:
sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
There is also a way to import submodules:
from sound.effects import echo
This will also import the submodule: mod:echo, and it does not need those lengthy prefixes, so it can be used like this:
echo.echofilter(input, output, delay=0.7, atten=4)
Another variation is to directly import a function or variable:
from sound.effects.echo import echofilter
Similarly, this method will import the submodule:mod:echo, and you can directly use its:func:echofilter function:
echofilter(input, output, delay=0.7, atten =4)
Note that when using from package import item, the corresponding item can be a submodule (subpackage) in the package, or other names defined in the package, such as functions, classes Or variables.
The import syntax will first treat item as the name of a package definition. If it is not found, it will try to import it as a module. If it hasn't been found yet, congratulations, an :exc:ImportError exception has been thrown.
On the contrary, if you use the import form such as import item.subitem.subsubitem, except for the last item, all must be packages, and the last item can be a module or a package, but it cannot be a class. , the name of the function or variable.
Import from a package*
Imagine what will happen if we use from sound.effects import *?
Python will enter the file system, find all the submodules in this package, and import them one by one.
But unfortunately, this method does not work very well on the Windows platform, because Windows is a case-insensitive system.
On this type of platform, no one dares to guarantee that a file called ECHO.py is imported as module: mod:echo or: mod:Echo or even: mod:ECHO.
(For example, Windows 95 annoyingly capitalizes the first letter of each file) and DOS's 8+3 naming rule makes the problem even more complicated when dealing with long module names.
In order to solve this problem, we can only trouble the package author to provide an accurate package index.
The import statement follows the following rules: If the package definition file __init__.py has a list variable called __all__, then when using from package import *, all names in this list will be imported as package content. .
#As the author of the package, don’t forget to ensure that __all__ is also updated after updating the package. You said I won't do this, I won't use import*, okay, no problem, who makes you the boss. Here is an example, file:sounds/effects/__init__.py contains the following code:
__all__ = ["echo", "surround", "reverse"]
This means that when you use from sound.effects import * this When using it, you will only import these three submodules in the package.
If __all__ is true but not defined, then when using the syntax from sound.effects import *, any submodules in the package: mod:sound.effects will *not* be imported. It just imports the package :mod:sound.effects and everything defined in it (possibly running the initialization code defined in :file:__init__.py).
This will import all the names defined in :file:__init__.py. And it won't break all the explicitly specified modules we imported before this sentence. Take a look at this part of the code:
import sound.effects.echo import sound.effects.surround from sound.effects import *
In this example, before executing from...import, the echo and surround modules in package:mod:sound.effects are imported into the current namespace. (Of course, it would be no problem if __all__ is defined)
Usually we do not advocate using * this method to import modules, because this method often leads to reduced code readability. However, this does save a lot of keystrokes, and some modules are designed to be imported only through specific methods.
Remember, there is never anything wrong with using from Package import specific_submodule. In fact, this is the recommended approach. Unless the submodule you want to import may have the same name as a submodule of another package.
If the package is a subpackage in the structure (for example, in this example for package: mod:sound), and you want to import sibling packages (packages of the same level), you have to use import absolute path to import. For example, if the module :mod:sound.filters.vocoder wants to use the module :mod:echo from the package :mod:sound.effects, you would write from sound.effects import echo.
from . import echo from .. import formats from ..filters import equalizer
Whether it is implicit or explicit relative import, it starts from the current module. The name of the main module is always "__main__". The main module of a Python application should always be referenced using an absolute path.
The package also provides an additional attribute, :attr:__path__. This is a directory list. Each directory included in it has a :file:__init__.py that serves this package. You must define other :file:__init__.py before it is executed. This variable can be modified to affect modules and subpackages contained in the package.
This function is not commonly used and is generally used to expand the modules in the package.