子模块无法导入父模块(python) [英] submodule couldn't import parentmodule (python)

查看:328
本文介绍了子模块无法导入父模块(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目录中运行该命令时,它只是告诉我一切都很好",但没有找到我的测试.)

To run the tests I did this (on the command line): python3 -m unittest test.test_file1. (When I ran that command for the whole test directory it just told me "everything is fine" but didn't find my tests.)

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

The answer (also in the command line, of course) was (without a huge part of the stacktrace):

   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

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

What to do to fix this? What's best-practice with tests in multiple files?

修改: 我尝试了建议的一些操作,这就是我所做的:

I tried a few things as suggested, and that's what I have done:

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

  1. I changed the tests direction (as suggested in the Hitchhikers guide to python. That's what it looks now:

modulewrapper
|-mymodule (with the 2 files in it)
|-... (docs, readME and this stuff)
|-tests
  |-test_file1.py
  |-test_file2.py

  • 我在插入了这样的方向后导入了mymodule(我在每个测试文件的开头都添加了此代码):

  • I imported mymodule after inserting the direction like this (I added this code in the beginning of every test file):

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


  • 我照常开始测试:python3 -m unittest test_file1从tests目录. 现在发生的事情是:(首先是stacktrace的相关部分,然后是我的猜测):


    I started the test as usual: python3 -m unittest test_file1 from the tests directory. What now happened is this: (first the relevant part of the stacktrace, then my guess):

    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
    

    如何处理这个新问题? (或与以前相同?更改工作代码以运行测试似乎不是一种最佳实践.)

    How to deal with that new problem? (or is it the same as before? It seems hardly to be a kind of best_practice to change working code to be able to run tests.)

    Edit2 :现在,我用from file2 import someclass替换了from .file2 import someclass.这会带来负面影响吗?

    For now, I jusr replaced from .file2 import someclass by from file2 import someclass. Can this have any negative side effects?

    推荐答案

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

    For the module test.test_file1, test is your top-level package. You can't go outside that for relative imports; you can only reach other modules within the test package.

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

    Either run python3 -m unittest mymodule.test.test_file1 (with your working directory set to the parent directory of mymodule) or add mymodule to your Python path and use from mymodule import file1.

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

    Generally, best practice is to put a tests directory next to your project:

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

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

    Note that there is no __init__.py file, but some test runners (specifically pytest, may require one).

    这就是Django项目本身与大多数其他Python项目一样组织测试的方式(例如,参见 click requests

    This is how the Django project itself organises tests, as do most other Python projects (see, for example, click, requests or flake8). Use a test runner to discover tests, like nose.

    当然,您需要将mymodule父目录添加到Python路径中才能起作用,因此,您可能需要添加

    You'd need to add your mymodule parent directory to the Python path for this to work, of course, so you may want to add

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

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

    to the top of your test files, or to put that into a utils.py file you put in your tests directory.

    另请参阅以下内容的 结构部分《 Python旅行者指南》 .

    Also see the Structure section of the Hitchhikers Guide to Python.

    这篇关于子模块无法导入父模块(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆