子模块无法导入父模块 (python)

submodule couldn't import parentmodule (python)

这些是我的模块的目录:

mymodule
|-__init__.py
|-file1.py
|-file2.py
|-test
  |-__init__.py
  |-test_file1.py
  |-test_file2.py

test_file1.py 包含此命令:from .. import file1.


为了 运行 我做了这个测试(在命令行上):python3 -m unittest test.test_file1。 (当我 运行 整个 test 目录的命令时,它只是告诉我 "everything is fine" 但没有找到我的测试。)

答案(当然也在命令行中)是(没有大部分堆栈跟踪):

   File "/media/me/my_usb/backup me/myfolder/django projects/django-mymodule/mymodule/test/test_file1.py", line 1, in <module>
    from .. import file1
ValueError: attempted relative import beyond top-level package

如何解决这个问题?在多个文件中进行测试的最佳做法是什么?


编辑: 我按照建议尝试了一些事情,这就是我所做的:

  1. 我更改了测试方向(如 Hitchhikers 指南中所建议的python。这就是现在的样子:

    modulewrapper
    |-mymodule (with the 2 files in it)
    |-... (docs, readME and this stuff)
    |-tests
      |-test_file1.py
      |-test_file2.py
    
  2. 我在插入方向后导入了mymodule(我在每个测试文件的开头添加了这段代码):

    import sys
    sys.path.insert(0, '../mymodule')
    import file1
    

我像往常一样开始测试:python3 -m unittest test_file1 从测试目录。 现在发生的事情是这样的:(首先是堆栈跟踪的相关部分,然后是我的猜测):

File "/media/me/my usb/backup me/my folder/django projects/django-mymodule/tests/test_file1.py", line 4, in <module>
    import file1
  File "../mymodule/file1.py", line 4, in <module>
    from .file2 import MyClass1, MyClass2
SystemError: Parent module '' not loaded, cannot perform relative import

如何处理那个新问题? (还是和以前一样?似乎很难best_practice更改工作代码以能够运行测试。)


Edit2: 现在,我只是用 from file2 import someclass 替换了 from .file2 import someclass。这会产生任何负面影响吗?

对于模块 test.test_file1test 是您的顶级包。您不能超出相对进口范围;您只能访问 test 包中的其他模块。

运行 python3 -m unittest mymodule.test.test_file1(将工作目录设置为 mymodule 的父目录)或将 mymodule 添加到 Python 路径并使用from mymodule import file1.

通常,最佳做法是在项目旁边放置一个 tests 目录

mymodule
|-__init__.py
|-file1.py
|-file2.py
tests
|-test_file1.py
|-test_file2.py

请注意,没有 __init__.py 文件,但一些 运行 测试人员(特别是 pytest,可能需要一个)。

这就是 Django 项目本身组织测试的方式,大多数其他 Python 项目也是如此(例如,参见 click, requests or flake8)。使用测试 运行ner 来发现测试,例如 nose.

当然,您需要将 mymodule 父目录添加到 Python 路径才能正常工作,因此您可能需要添加

import os
import sys
here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(here))

到测试文件的顶部,或者将其放入测试目录中的 utils.py 文件中。

另见 Structure section of the Hitchhikers Guide to Python