对相关python进口的极限答案

对相关python进口的极限答案,第1张

概述我知道 Python中有相同的导入问题有很多问题,但似乎没人设法提供正确使用的明确示例. 假设我们有一个包含两个模块foo和bar的软件包.里面的foo我们需要能够访问吧. 因为我们还在开发它,所以mypackage不在sys.path中. 我们希望能够: > import mypackage.foo >运行foo.py作为脚本,并从__main__部分执行示例使用或测试. >使用Python 2 我知道 Python中有相同的导入问题有很多问题,但似乎没人设法提供正确使用的明确示例.

假设我们有一个包含两个模块foo和bar的软件包.里面的foo我们需要能够访问吧.

因为我们还在开发它,所以mypackage不在sys.path中.

我们希望能够:

> import mypackage.foo
>运行foo.py作为脚本,并从__main__部分执行示例使用或测试.
>使用Python 2.5

为了确保在所有这些情况下都能正常工作,我们必须在foo.py中进行导入.

# mypackage/__init__.py...# mypackage/foo/__init__.py...# mypackage/bar.py  def dobar()    print("dobar")# mypackage/foo/foo.pyimport bar # fails with module not foundimport .bar #fails due to ValueError: Attempted relative import in non-packagedef doFoo():    print(dobar())if __name__ == '__main__':    doFoo()
解决方法 看看 PEP 328的以下信息:

relative imports use a module’s __name__ attribute to determine that module’s position in the package hIErarchy. If the module’s name does not contain any package information (e.g. it is set to '__main__') then relative imports are resolved as if the module were a top level module,regardless of where the module is actually located on the file system.

当您将foo.py作为脚本运行时,该模块的__name__是“__main__”,因此您不能进行相对导入.即使mypackage在sys.path上也是如此.基本上,如果导入了该模块,则只能从模块进行相对导入.

以下是解决这个问题的几个选择:

1)在foo.py中,检查是否__name__ ==’__main__’,并有条件地将mypackage添加到sys.path中:

if __name__ == '__main__':    import os,sys    # get an absolute path to the directory that contains mypackage    foo_dir = os.path.dirname(os.path.join(os.getcwd(),__file__))    sys.path.append(os.path.normpath(os.path.join(foo_dir,'..','..')))    from mypackage import barelse:    from .. import bar

2)始终使用mypackage导入栏导入栏,并执行foo.py,使得mypackage可以自动显示:

$cd <path containing mypackage>$python -m mypackage.foo.foo
总结

以上是内存溢出为你收集整理的对相关python进口极限答案全部内容,希望文章能够帮你解决对相关python进口的极限答案所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1206759.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存