Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的条件相对导入。。。做还是不做?_Python_Import_Conditional Statements_Relative Path_Python Packaging - Fatal编程技术网

Python中的条件相对导入。。。做还是不做?

Python中的条件相对导入。。。做还是不做?,python,import,conditional-statements,relative-path,python-packaging,Python,Import,Conditional Statements,Relative Path,Python Packaging,考虑到以下包: testpackage __init__.py testmod.py testmod2.py \uuuu init\uuuuu.py的内容 from . import testmod from . import testmod2 # relative import without conditional logic, breaks when run directly from . import testmod2 ... if __name__ == '

考虑到以下包:

testpackage
    __init__.py
    testmod.py
    testmod2.py
\uuuu init\uuuuu.py的内容

from . import testmod
from . import testmod2
# relative import without conditional logic, breaks when run directly
from . import testmod2
...
if __name__ == '__main__':
    # do my module testing here
    # this code will never execute since the relative import 
    # always throws an error when run directly
if __name__ == 'testpackage.testmod2':
    from . import testmod
else:
    import testmod
...
if __name__ == '__main__':
    # test code here, will execute when the file is run directly
    # due to conditional imports
testmod.py的内容

from . import testmod
from . import testmod2
# relative import without conditional logic, breaks when run directly
from . import testmod2
...
if __name__ == '__main__':
    # do my module testing here
    # this code will never execute since the relative import 
    # always throws an error when run directly
if __name__ == 'testpackage.testmod2':
    from . import testmod
else:
    import testmod
...
if __name__ == '__main__':
    # test code here, will execute when the file is run directly
    # due to conditional imports
testmod2.py的内容

from . import testmod
from . import testmod2
# relative import without conditional logic, breaks when run directly
from . import testmod2
...
if __name__ == '__main__':
    # do my module testing here
    # this code will never execute since the relative import 
    # always throws an error when run directly
if __name__ == 'testpackage.testmod2':
    from . import testmod
else:
    import testmod
...
if __name__ == '__main__':
    # test code here, will execute when the file is run directly
    # due to conditional imports

这不好吗?有更好的方法吗?

这肯定会成为未来维护的难题。不仅仅是有条件的导入。。。您必须执行条件导入的更多原因是,作为主脚本运行
testpackage/testmod2.py
导致
sys.path
的第一个条目是
/testpackage
,而不是
,这使得testpackage作为一个包的存在消失了

相反,我建议通过
python-mtestpackage.testmod2运行testmod2,并从外部
testpackage
执行
testpackage.testmod2
仍将显示为
\uuuu main\uuuu
,但条件导入将始终有效,因为
testpackage
始终是一个包


-m
的问题在于它需要。

我感觉到它将来会破损,还有一个维护问题。我建议您不要这样做。它在代码中,但要概括一下:当您直接运行模块时,相对模块导入会抛出ValueError,从而取消了运行测试代码的能力。通过添加条件逻辑来检查模块是直接运行还是作为包的一部分导入,它允许我在仍然使用相对导入的情况下保留直接运行脚本的能力。我直接运行模块的唯一原因是为了测试/调试。在VIM中,我绑定-e以直接执行当前的python文件,在将代码添加到模块时,我经常使用该文件来测试代码。也许更好的解决方案是创建一个自定义vim文件,在加载使用上面引用的-m标志执行模块的项目时,我将其作为源文件。