保留的 Python 模块/包名称是什么?

Dav*_*ave 6 python unit-testing python-3.x

我在使用 Python 单元测试时遇到了一个奇怪的错误。我的项目中有两个文件夹:

project
    code
        __init__.py        (empty)
        app.py             (defines my App class)
    test
        test.py            (contains my unit tests)
Run Code Online (Sandbox Code Playgroud)

测试.py是:

import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from code.app import App

class test_func1(unittest.TestCase):
    ...
Run Code Online (Sandbox Code Playgroud)

当我运行 test.py 时,我收到消息:

Traceback (most recent call last):
    File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...test.py, line 5, in <module>
    from code.app import App
ImportError: No module named 'code.app': 'code' is not a package
Run Code Online (Sandbox Code Playgroud)

在验证了它的__init__.py存在并敲了我的头一段时间后,我一时兴起将应用程序目录的名称从 code 更改为 prog:

import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from prog.app import App
Run Code Online (Sandbox Code Playgroud)

……一切突然都好起来了。Unittest 正确导入了我的应用程序并运行了测试。

我已经搜索过https://docs.python.org/3.5/reference/lexical_analysis.html#keywordshttps://docs.python.org/3/reference/import.html#path-entry-finders和 don没有看到任何code非法目录名称的迹象。这将在哪里记录,以及保留哪些其他目录名称?

系统:win32、Windows 7 上的 python 3.4.3 [MSC v1600 32 位]

che*_*ner 6

code不是保留的,但它已经在标准库中定义了,它是常规模块而不是包要从包中导入,您应该使用相对导入。

from .code.app import App
Run Code Online (Sandbox Code Playgroud)