我在python导入父包时遇到问题。
考虑单元测试,例如:
import unittest
from parent.child.grandchildren import some_code
class SomeCodeTest(unittest.TestCase):
def test_that_some_code_get_stuff_done(self):
self.assertEqual(some_code("stuff"), "stuff done")
if __name__ == "__main__":
unittest.main()
现在,如果例如parent/__init__.py
包含:
import os
foo = os.environ["AN_ENV_VARIABLE"]
如果未设置该变量,则我的单元测试将失败,因为python会导入父包。 是否有某种方法可以解决此问题而无需修改生产代码?我只想导入该单元以及该单元,而不论相邻代码多么丑陋或错误。