简体   繁体   English

包内的Python3相对导入

[英]Python3 Relative Imports inside a package

I realize that there are a lot of questions about this on StackOverflow already, but I find the solutions to be entirely unclear and often contradicting of each other. 我意识到在StackOverflow上已经有很多关于此的问题,但是我发现解决方案是完全不清楚的,并且常常彼此矛盾。

I have the following scenario: I am writing a python (Python 3.4) package with the following structure: 我有以下情形:我正在编写具有以下结构的python(Python 3.4)包:

mypackage/
    __init__.py (empty file)
    mydir1/
        __init__.py (empty file)
        mymodule1.py
    mydir2/
        __init__.py (empty file)
        mymodule21.py
        mymodule22.py
    mydir3/
        __init__.py (empty file)
        mymodule31.py
        mymodule32.py
    tests/
        __init__.py (empty file)
        test_something_in_mydir1.py
        test_something_in_mydir2.py
        test_something_in_mydir3.py

I want the files within the tests/ directory to contain unit tests of everything inside the package. 我希望tests/目录中的文件包含包中所有内容的单元测试。 The problem is that no matter which approach I try, I get this error: 问题是,无论我尝试哪种方法,都会出现此错误:

SystemError: Parent module '' not loaded, cannot perform relative import

I should note that I want this to work "out of the box" which means that I do not want to import things using their absolute paths and I do not want to change my path variable. 我应该注意,我想让它“开箱即用”工作,这意味着我不想使用它们的绝对路径导入事物,也不想更改路径变量。 None of these solutions seem very pythonic and its driving me slowly up the wall. 这些解决方案似乎都不是非常出色,它使我慢慢地陷入困境。 Is there really no way to do this? 真的没有办法做到这一点吗?

I've tried a bunch of stuff already, including examples below, but they all seem to produce the same result: 我已经尝试了很多东西,包括下面的示例,但是它们似乎都产生了相同的结果:

from ..mydir1.mymodule1 import *
from .mypackage.mydir1.mymodule1 import *
from ...mypackage.mydir1.mymodule1 import *

And I've tried overwriting the __all__ variable in each of the __init__.py files as well as doing the imports as shown here: https://docs.python.org/3/reference/import.html#submodules 而且我尝试覆盖每个__init__.py文件中的__all__变量,以及进行导入,如下所示: https : //docs.python.org/3/reference/import.html#submodules

Can anyone tell me how to structure the imports (for example) in the file test_something_in_mydir1.py in such a way as to have access to all the classes/functions that are found in mymodule1.py for unit testing the package? 谁能告诉我如何构造文件test_something_in_mydir1.py中的导入(例如),以便可以对mymodule1.py中找到的所有类/函数进行访问以对包进行单元测试?

If test_something_in_mydir1.py contains: 如果test_something_in_mydir1.py包含:

import unittest
from ..mydir1 import mymodule1

class Test1(unittest.TestCase):
    def test1(self):
        self.assertEqual(mymodule1.x,1) # mymodule1 trivially contains x=1

And if the unit tests were launched from the directory containing the package (in this case, C:\\Test ) using the following command: 并且如果使用以下命令从包含软件包的目录(在本例中为C:\\Test )中启动了单元测试:

py -m unittest discover -v

The tests are discovered and run correctly with relative imports: 测试被发现并通过相对导入正确运行:

C:\Test>tree /f
Folder PATH listing
Volume serial number is CE8B-D448
C:.
└───mypackage
    │   __init__.py
    │
    ├───mydir1
    │       mymodule1.py
    │       __init__.py
    │
    ├───mydir2
    │       mymodule21.py
    │       mymodule22.py
    │       __init__.py
    │
    ├───mydir3
    │       mymodule31.py
    │       mymodule32.py
    │       __init__.py
    │
    └───tests
            test_something_in_mydir1.py
            __init__.py


C:\Test>py -m unittest discover -v
test1 (mypackage.tests.test_something_in_mydir1.Test1) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM