[英]Relative import error using python unittest
我正在使用内置的 python 单元测试模块。 我的目录结构如下:
game.py
test/test_game.py
test_game.py 的内容:
import unittest
from ..game import *
class TestGame(unittest.TestCase):
def test_pawn(self):
game = Game()
game.make_move("e2e3")
self.assertEqual(game.board[6][5].piece_type, "P")
if __name__ == '__main__':
unittest.main()
这就是问题所在。 当我运行python -m unittest test/test_game.py
我得到错误:
ImportError: Failed to import test module: test_game
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'test.test_game'
我错过了test/__init__.py
文件。 有了这个改变,我什至不需要相对的..game
导入。但我想知道为什么 python 只需要这个来导入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.