2
我有一個calc.py文件,其中有基本計算方法。現在,我創建另一個文件(位於同一目錄中),名爲test_calc.py,用於單元測試calc.py file.But當我嘗試無論是通過命令行使用Python在嘗試將模塊導入另一個模塊時生成SystemError
python3 -m unittest test_calc.py
運行test_calc.py或因爲我已經包括名 == 「主要」
python3 test_calc.py
或嘗試直接通過IDE運行它,我得到的是說
from . import calc
SystemError: Parent module '' not loaded, cannot perform relative import
下面的錯誤是我的項目結構
下面的截圖是我如何導入calc.py截圖文件並接受進口
這是test_calc.py文件中的代碼其中U nitests在calc.py文件
import unittest
from . import calc
class TestCalc(unittest.TestCase):
def test_add(self):
result = calc.add(5,2)
self.assertEqual(result,7)
if __name__ == "__main__":
unittest.main()
中定義的方法。當我運行上面的代碼,Python的thows error.What哪裏出問題了?
「至少在'unittest模塊'文件夾中沒有空格。」 ---這解決了問題!再次感謝! –
我將文件夾重命名爲unittest_module.So,而不是從。導入calc,我從unittest_module導入了calc並且工作正常。 –
@SouvikRay很好的解決方案:這看起來更簡單。 – VonC