在python中,如何知道何时必须导入子模块与主模块?

汉克·戈登

我对此有一个后续的初学者问题:是否需要直接导入子模块?

我怎么知道子模块的导入是显式的?我可以在父母模块中使用help()或dir()找到它吗?如果是的话,在哪里?

布布

简短的答案是您应该可以使用dir(),但不能使用help()

长答案:

让我们以multiprocessingPython 3.8.5模块为例(这就是我所拥有的)。我的安装的目录结构部分是:

Python38
    Lib
        multiprocessing
            dummy
                __init__.py
                connection.py
            __init__.py
            pool.py

现在,我导入multiprocessing模块dir并对它执行a操作,并观察到既没有dummy也不pool出现模块:

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import multiprocessing
>>> dir(multiprocessing)
['Array', 'AuthenticationError', 'Barrier', 'BoundedSemaphore', 'BufferTooShort', 'Condition', 'Event', 'JoinableQueue', 'Lock', 'Manager', 'Pipe', 'Pool', 'Process', 'ProcessError', 'Queue', 'RLock', 'RawArray', 'RawValue', 'SUBDEBUG', 'SUBWARNING', 'Semaphore', 'SimpleQueue', 'TimeoutError', 'Value', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'active_children', 'allow_connection_pickling', 'context', 'cpu_count', 'current_process', 'freeze_support', 'get_all_start_methods', 'get_context', 'get_logger', 'get_start_method', 'log_to_stderr', 'parent_process', 'process', 'reducer', 'reduction', 'set_executable', 'set_forkserver_preload', 'set_start_method', 'sys']

而且,可以肯定的是,如果我尝试访问这些模块,则会收到错误消息:

>>> multiprocessing.pool
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'multiprocessing' has no attribute 'pool'
>>> multiprocessing.dummy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'multiprocessing' has no attribute 'dummy'

但是如果我发出help我得到(部分):

>>> help(multiprocessing)
Help on package multiprocessing:

NAME
    multiprocessing

MODULE REFERENCE
    https://docs.python.org/3.8/library/multiprocessing

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    # Package analogous to 'threading.py' but using processes
    #
    # multiprocessing/__init__.py
    #
    # This package is intended to duplicate the functionality (and much of
    # the API) of threading.py but uses processes instead of threads.  A
    # subpackage 'multiprocessing.dummy' has the same API but is a simple
    # wrapper for 'threading'.
    #
    # Copyright (c) 2006-2008, R Oudkerk
    # Licensed to PSF under a Contributor Agreement.
    #

PACKAGE CONTENTS
    connection
    context
    dummy (package)
    forkserver
    heap
    managers
    pool
    popen_fork
    (rest of listing ommitted)

您可以看到dummypool包含了这些子模块,但是我们知道我必须显式导入这些子模块。

现在,我们从列出dir清单中注意到context,它也列在help清单中命名的软件包中因此,我应该能够访问它而无需任何进一步的导入,并且我可以:

>>> multiprocessing.context
<module 'multiprocessing.context' from 'C:\\Program Files\\Python38\\lib\\multiprocessing\\context.py'>

最后:

>>> from multiprocessing.pool import Pool
>>> from multiprocessing.dummy import Pool
>>>

最终,仅通过提供示例,文档应告诉您需要导入的内容。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章