Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python3 module


May 10, 2021 Python3


Table of contents


In the previous chapters we scripted with the Python interpreter, and if you exit and re-enter from the Python interpreter, all the methods and variables you define disappear.

To do this, Python provides a way to store these definitions in a file for use by some script or interactive interpreter instance, known as a module.

A module is a file that contains all the functions and variables that you define, and the suffix is .py. A module can be introduced by another program to use functions such as functions in the module. T his is also the way to use the Python standard library. T he following is an example of using modules in the Python Standard Library.

#!/usr/bin/python3
# Filename: using_sys.py

import sys

print('命令行参数如下:')
for i in sys.argv:
   print(i)

print('\n\nPython 路径为:', sys.path, '\n')

The 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;
  • 2, sys.argv is a list of command line parameters.
  • 3, sys.path contains a list of paths for the Python interpreter to automatically find the desired module.

Import statement

If you want to use a Python source file, simply execute the import statement in another source file, and the import statement syntax is as follows:

import module1[,module2[, ... moduleN]

When the interpreter encounters an import statement, if the module is imported in the current search path.

The search path is a list of all directories that the interpreter searches first. If you want to import module support, you need to put the command at the top of the script:

#!/usr/bin/python3
# Filename: support.py

def print_func( par ):
    print ("Hello : ", par)
    return

test.py to introduce the support module:

#!/usr/bin/python3
# Filename: test.py

# 导入模块
import support

# 现在可以调用模块里包含的函数了
support.print_func("w3cschool")

The above example output results:

$ python3 test.py
Hello :  w3cschool

A module is imported only once, no matter how many times you execute the import. This prevents the import module from being executed over and over again.

How does the Python interpreter find the corresponding file when we use the import statement?

This involves Python's search path, which consists of a series of directory names from which the Python interpreter looks for the modules introduced.

This looks a lot like an environment variable, and in fact, you can determine the search path by defining the environment variable.

The search path is determined when Python compiles or installs, and installing a new library should also be modified. T he search path is stored in the path variable in the sys module, a simple experiment, and 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']
>>> 

The sys.path output is a list in which the first item is an empty string '', which represents the current directory (which directory is more clearly available if printed from a script), or the directory where we execute the Python interpreter (for which the script is located).

So if, like me, there is a file in the current directory with the same name as the module to be introduced, the module to be introduced is masked.

With an understanding of the concept of search paths, you can modify sys.path in your script to introduce modules that are not in the search path.

Now, in the interpreter's current directory or in a directory in sys.path, create a fibo.py file with the following code:

# 斐波那契数列模块(Fibonacci numbers module)

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 go to the Python interpreter and import the module using the following command:

>>> import fibo

This does not write the function name defined directly in fibo to the current symbol table, but simply writes the name of the module fibo there.

You can use the module name to access the function:

>>> 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 often, you can assign it to 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 lets you import a specified portion from the module into the current namespace with the following syntax:

from modname import name1[, name2[, ... nameN]

For example, to import the fib function of the module fibo, use the following statement:

>>> from fibo import fib, fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This declaration does not import the entire fibo module into the current namespace, it only introduces the fib function in the fibo.


from ... Import s statement

It is also possible to import everything from a module into the current namespace, using only the following declaration:

from modname import *

This provides an easy way to import all the items in a module. However, such statements should not be used too much.


Learn more about modules

Modules can include executable code in addition to method definitions. T his code is typically used to initialize the module. The code is executed only the first time it is imported.

Each module has its own separate symbol table, which is used as a global symbol table for all functions within the module.

Therefore, the author of the module can use these global variables boldly inside the module without worrying about spending the global variables of other users.

On the other hand, when you do know what you're doing, you can also access functions within the module through notations like modname.itemname.

Modules can be imported into other modules. I mport is used to import a module at the front of a module (or script, or elsewhere), which is of course a convention, not mandatory. The name of the imported module is placed in the symbol table of the module currently in operation.

There is also an import method that allows you to import the name of the module (function, variable) directly into the current operating module. Like what:

>>> from fibo import fib, fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This imported method does not place the name of the imported module in the current character table (so in this case, the fibo name is undefined).

There is also a way to import all (function, variable) 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 in, but those names that start with a single underscore are not in this case. In most cases, Python programmers do not use this method because the naming of other sources introduced is likely to override existing definitions.


__name__ properties

When a module is first introduced by another program, its main program runs. If we want a block in a module not to execute when a module is introduced, we can use the __name__ property to make the block execute only when the module itself is running.

#!/usr/bin/python3
# Filename: using_name.py

if __name__ == '__main__':
 print('程序自身在运行')
else:
 print('我来自另一模块')

The running output is as follows:

$ python using_name.py
程序自身在运行
$ python
>>> import using_name
我来自另一模块
>>>

Description:

  1. Each module has a __name__ property that, when its value is '__main__', indicates that the module itself is running, otherwise it is introduced.
  2. __name__ and __main__ are double underscores, which are """


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 argument is given, the dir() function lists 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 carries some standard module libraries, which will be covered in the Python library reference documentation (that is, the "library reference documentation" that follows).

Some modules are built directly into the parser, which is not a language-built feature, but it can be used efficiently, even at the system level.

These components are configured in different forms depending on the operating system, such as winreg, which is only available to Windows systems.

It should be noted that this has a special module sys, which is built into every Python parser. The variables sys.ps1 and sys.ps2 define the strings corresponding to the main and sub-prompts:

>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps1 = 'C> '
C> print('Yuck!')
Yuck!
C>

Package

A package is a form of managing the Python module namespace with a "dot module name."

For example, if the name of a .B A, he says sub-module B in package A.

Just as when you use modules, you don't have to worry about global variables affecting each other between different modules, and you don't have to worry about module renames between different libraries in the form of point module names.

So different authors can provide NumPy modules, or Python graphics libraries.

Suppose you want to design a module (or "package") that processes sound files and data uniformly.

There are many different audio file formats (basically distinguished by suffix names, e.g. .wav,:file:.aiff,:file:.au), so you need to have an ever-increasing set of modules to convert between different formats.

And for this audio data, there are many different operations (such as mixing, adding echo, adding equalizer functionality, creating artificial stereo effects), so you also need a set of modules that can't be written 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 looks for the subdirectdirections contained in the package based on the directories in sys.path.

A directory can only be considered a package if it contains a file called __init__.py, primarily to avoid the accidental impact of some vulgar name (such as string) on a valid module in the search path.

In the simplest case, put an empty : file:__init__.py on it. Of course, this file can also contain some initialization code or assign values to variables (__all__ later).

Users can import only specific modules in one package at a time, such as:

import sound.effects.echo

This will import sub-modules: mod:song.effects.echo. He must use his full name to access:

sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)

Another way to import sub-modules is to:

from sound.effects import echo

This also imports sub-module echo, and he doesn't need those lengthy prefixes, so he can use them this way:

echo.echofilter(input, output, delay=0.7, atten=4)

Another change is to import a function or variable directly:

from sound.effects.echo import echofilter

Similarly, this approach imports the sub-module echo and can use his echofilter() function directly:

echofilter(input, output, delay=0.7, atten=4)

Note When using the form of from package import item, the corresponding item can be either a sub-module (sub-package) in the package, or other names defined in the package, such as functions, classes, or variables.

The import syntax first treats item as a package-defined name and, if not found, tries to import it as a module. If you haven't found it yet, congratulations, an exc:ImportError exception has been thrown.

Conversely, if you use an import form such as import item.subitem.subsubitem, all but the last item must be a package, and the last item can be a module or a package, but not the name of a class, function, or variable.


Import from a package

Imagine what would happen if we used from sound.effects import?

Python goes into the file system and finds all the sub-modules in this package, one by one, and imports them in.

Unfortunately, this approach doesn't work very well on Windows platforms, because Windows is a case-insenseable system.

On such platforms, no one can guarantee whether a file called ECHO.py imported into the module echo or even ECHO.

(For example, Windows 95 hates to capitalise the initials of each file) and the DOS's 8-3 naming rules handle long module names to make the problem more tangled.

In order to solve this problem, the author can only bother to provide an accurate packet index.

The import statement follows the following rule: if the package definition file __init__.py has a list variable called __all__, all the names in the list are imported as package content when you use from package import.

As the author of the package, don't forget to make sure that __all__ the package is updated. I f you say I'm not going to do this, I'm not going to use import, okay, no problem, who makes you the boss? Here's an example that :file:sounds/effects/__init__.py

__all__ = ["echo", "surround", "reverse"]

This means that when you use from sound.effects import, you will only import the three sub-modules in the package.

If __all__ is true and undefined, any sub-modules in the package sound.effects are not imported when using the syntax of from sound.effects import. He simply imports the package sound.effects and everything defined in it (possibly running the initialization code defined in __init__.py).

This will __init__.py names defined in the list. A nd he won't destroy all the explicitly specified modules we imported before this sentence. Look at this part of the code:

import sound.effects.echo
import sound.effects.surround
from sound.effects import *

In this example, in the execution of from... B efore import, both the echo and surround modules in the package sound.effects were imported into the current namespace. (Of course, if you define __all__, it's even better))

In general, we don't advocate importing modules using this method, which often results in less readability of the code. However, this can indeed save a lot of keystrokes, and some modules are designed to be imported only by specific methods.

Keep in mind that there's never specific_submodule this approach when you use from Package import. I n fact, this is also the recommended method. Unless it is possible for the sub-module you want to import to be renamed with the sub-module of the other package.

If the package is a sub-package in the structure (for example, for package sound in this case), and you want to import the brother package (the same level package), you have to import it using the import absolute path. For example, if the module sound.filters.vocoder wants to use the module echo in the package sound.effects, you'll write from.effects import echo.

from . import echo
from .. import formats
from ..filters import equalizer

Both implicit and explicit relative imports start with the current module. The name of the main module is always "__main__", the main module of a Python application, and absolute path references should always be used.

The package also provides an additional property __path__. T his is a list of directories, each containing a __init__.py that serves the package, which you have to define before __init__.py are executed. You can modify this variable to affect the modules and sub-packages that are included in the package.

This function is not commonly used and is generally used to extend the modules in the package.