Python 3.x 两个新的(?)变种的共同;ImportError:没有名为“的模块”;测试时出现异常
和许多其他人一样,我在测试返回错误“ImportError:没有名为{module name}的模块”时遇到了麻烦。这通常是因为测试与正在测试的代码位于一个单独的目录中。修复包括确保文件夹中存在_uinit__;语句,将测试与测试模块放在同一文件夹中,或者修改python路径的技巧。这类问题的一个例子是: 我得出的结论是,我的系统中的PYTHONPATH出现了严重问题,因为:Python 3.x 两个新的(?)变种的共同;ImportError:没有名为“的模块”;测试时出现异常,python-3.x,unit-testing,pycharm,anaconda,pytest,Python 3.x,Unit Testing,Pycharm,Anaconda,Pytest,和许多其他人一样,我在测试返回错误“ImportError:没有名为{module name}的模块”时遇到了麻烦。这通常是因为测试与正在测试的代码位于一个单独的目录中。修复包括确保文件夹中存在_uinit__;语句,将测试与测试模块放在同一文件夹中,或者修改python路径的技巧。这类问题的一个例子是: 我得出的结论是,我的系统中的PYTHONPATH出现了严重问题,因为: 即使将测试放在它们测试的模块旁边,测试现在也不起作用 与正在测试的模块位于同一文件夹中的测试脚本实际上可以导入和使用模
- 即使将测试放在它们测试的模块旁边,测试现在也不起作用
- 与正在测试的模块位于同一文件夹中的测试脚本实际上可以导入和使用模块,只要它们作为python可执行文件运行,而不是由测试(例如pytest)运行
- Kenneth Reitz的《Python搭便车指南》演示的操作系统路径修复程序,使用从GitHub克隆的包,在我的系统上也不起作用
pydnmr
├── CHANGELOG.txt
├── LICENSE.txt
├── README.rst
├── docs
│ └── placeholder.txt
├── pydnmr
│ ├── __init__.py
│ ├── dnmrmath.py
│ ├── dnmrplot.py
│ ├── main.py
│ ├── plottools.py
│ ├── test_dnmrmath.py
│ └── test_plot.py
├── requirements.txt
├── setup.py
└── tests # unused
├── __init__.py
├── context.py
└── test_plot.py
我最初尝试在tests文件夹中使用Reitz的方法,但没有成功,所以我放弃了,并将测试移动到与要测试的代码相同的目录(dnmrplot.py等)。如果测试文件作为python脚本从命令行运行(python test\u plot.py
),则会导入模块并正常运行,但如果作为测试运行(pytest test\u plot.py
),则会出现导入错误
以下是test_plot.py的代码:
import pytest
# noinspection PyUnresolvedReferences
import dnmrplot
def test_dnmrplot_2spin_type():
WINDNMR_DEFAULT = (165.00, 135.00, 1.50, 0.50, 0.50, 0.5000)
x, y = dnmrplot.dnmrplot_2spin(*WINDNMR_DEFAULT)
print('x ', type(x), "y", type(y))
assert type(x) == "<class 'numpy.ndarray'>"
assert type(y) == "<class 'numpy.ndarray'>"
if __name__ == "__main__":
import plottools as pt
WINDNMR_DEFAULTS = (165.00, 135.00, 1.50, 0.50, 0.50, 50.00)
spectrum = dnmrplot.dnmrplot_2spin(*WINDNMR_DEFAULTS)
pt.popplot(*spectrum)
但是,他包含的testtest_basic.py
实际上并没有测试导入。我对其进行了修订,以便:
from .context import sample
import unittest
class BasicTestSuite(unittest.TestCase):
"""Basic test cases."""
def test_absolute_truth_and_meaning(self):
assert True
# I Added the following to actually require accessing 'sample':
def test_sample_hmm(self):
response = sample.hmm()
assert response == 'hmmm...'
if __name__ == '__main__':
# unittest.main() #Reitz' code
print(sample.hmm()) # my code
当作为python脚本或测试运行时,它无法导入示例:
def test_sample_hmm(self):
response = sample.hmm()
> assert response == 'hmmm...'
E AssertionError: assert None == 'hmmm...'
test_basic.py:18: AssertionError
----------------------------- Captured stdout call -----------------------------
hmmm...
====================== 1 failed, 1 passed in 0.06 seconds ======================
$ python test_basic.py
Traceback (most recent call last):
File "test_basic.py", line 3, in <module>
from .context import sample
SystemError: Parent module '' not loaded, cannot perform relative import
$ cd ..
$ python tests/test_basic.py
Traceback (most recent call last):
File "tests/test_basic.py", line 3, in <module>
from .context import sample
SystemError: Parent module '' not loaded, cannot perform relative import
def-test\u-sample\u-hmm(自):
response=sample.hmm()
>断言响应=='hmmm..'
E AssertionError:assertnone=='hmmm..'
test_basic.py:18:AssertionError
-----------------------------捕获的stdout调用-----------------------------
六羟甲基三聚氰胺六甲醚。。。
============================1失败,1在0.06秒内通过======================
$python test_basic.py
回溯(最近一次呼叫最后一次):
文件“test_basic.py”,第3行,在
from.context导入示例
SystemError:未加载父模块“”,无法执行相对导入
$cd。。
$python tests/test_basic.py
回溯(最近一次呼叫最后一次):
文件“tests/test_basic.py”,第3行,在
from.context导入示例
SystemError:未加载父模块“”,无法执行相对导入
我尝试过通过命令行运行测试,包括测试文件内部和上面的测试。我还尝试在IDE PyCharm中运行测试。我已经尝试使用PyCharm将上面的pydnmr文件夹设置为源根目录。我摆弄了一下Reitz的变通方法。我尝试将我的项目克隆到硬盘上完全不同的位置。在这一点上,我的假设是,在我的Mac上安装Anaconda和使用PyCharm之间,我的机器上的PYTHONPATH出现了问题,我需要一个驱魔师
在OS X上使用Python3.4,并安装Anaconda和PyCharm
如果您能帮助我找出导入问题的根源,我们将不胜感激。问题的根源似乎在于模块是用作导入还是用作脚本。这似乎解决了以下问题:
我发现了一些东西:当使用常规导入时,python脚本作为程序运行,但a)测试失败,b)导入在PyCharm中被标记为未解析引用,并带有红色警告。但是如果使用了相对(点)导入,测试工作正常,PyCharm没有发现错误,但是python程序不会运行!e、 g在test_plot.py中使用
from.dnmrplot import dnmrplot_2spin
并相应地更改引用会导致测试(命令行或PyCharm中)通过,但python test_plot.py
给出系统错误:未加载父模块“”,无法执行相对导入。
def test_sample_hmm(self):
response = sample.hmm()
> assert response == 'hmmm...'
E AssertionError: assert None == 'hmmm...'
test_basic.py:18: AssertionError
----------------------------- Captured stdout call -----------------------------
hmmm...
====================== 1 failed, 1 passed in 0.06 seconds ======================
$ python test_basic.py
Traceback (most recent call last):
File "test_basic.py", line 3, in <module>
from .context import sample
SystemError: Parent module '' not loaded, cannot perform relative import
$ cd ..
$ python tests/test_basic.py
Traceback (most recent call last):
File "tests/test_basic.py", line 3, in <module>
from .context import sample
SystemError: Parent module '' not loaded, cannot perform relative import