在python中使用absolute_import和处理相对模块名称冲突 [英] Using absolute_import and handling relative module name conflicts in python

查看:75
本文介绍了在python中使用absolute_import和处理相对模块名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望这是一个简单的案例,我没有理解复杂的 Python2 导入机制.我有以下设置:

$>ls -ltr pypackage1共 3 个-rw-r--r-- 1 pelson pelson 0 Aug 17 19:20 io.py-rw-r--r-- 1 pelson pelson 0 Aug 17 19:20 __init__.py-rw-r--r-- 1 pelson pelson 57 Aug 17 19:22 code.py$>猫 pypackage1/code.py从 __future__ 导入 absolute_import导入压缩文件

即我只有一个存根包,其中包含一个空的 __init__.pyio.py,以及一个 2 行的 code.py 文件.>

我可以导入pypackage1:

$>python -c导入pypackage1.code"

但我无法运行 code.py 文件:

$>蟒蛇pypackage1/code.py回溯(最近一次调用最后一次): 中的文件pypackage1/code.py",第 3 行导入压缩文件 中的文件python2.7/zipfile.py",第 462 行类 ZipExtFile(io.BufferedIOBase):AttributeError: 'module' 对象没有属性 'BufferedIOBase'

显然问题与 zipfile 模块通过内置 io 模块获取我的相对 io 模块有关,但我认为我的 from __future__ import absolute_import 本来可以解决这个问题的.

在此先感谢您的帮助,

解决方案

这是正确的行为.如果您想修复错误,请不要从包内部运行.

当您运行包内的脚本时,python 不会将该目录解释为包,从而将工作目录添加到 PYTHONPATH.这就是为什么 zipfile 模块导入的 io 模块是你的 io 模块,而不是标准库中的模块.

我建议在您的包外(或在 bin/scripts 文件夹中)创建一个简单的启动器脚本,然后启动它.此脚本可以简单地包含以下内容:

from pypackage1 导入代码代码.main()

<小时>

另一种方法是告诉 python 解释器您要执行的文件是模块的一部分.您可以使用 -m 命令行选项执行此操作.在您的情况下,您必须执行以下操作:

python -m pypackage1.code

注意-m的参数应该是模块名,而不是文件名.

I really hope this is a simple case of me miss-understanding the complex Python2 import mechanisms. I have the following setup:

$> ls -ltr pypackage1 
total 3
-rw-r--r-- 1 pelson pelson   0 Aug 17 19:20 io.py
-rw-r--r-- 1 pelson pelson   0 Aug 17 19:20 __init__.py
-rw-r--r-- 1 pelson pelson  57 Aug 17 19:22 code.py
$> cat pypackage1/code.py 
from __future__ import absolute_import

import zipfile

i.e. I have nothing but a stub package with an empty __init__.py and io.py, and a 2 lines code.py file.

I can import pypackage1:

$> python -c "import pypackage1.code"

But I cannot run the code.py file:

$> python pypackage1/code.py
Traceback (most recent call last):
  File "pypackage1/code.py", line 3, in <module>
    import zipfile
  File "python2.7/zipfile.py", line 462, in <module>
    class ZipExtFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'

Clearly the problem has to do with the zipfile module picking up my relative io module over the builtin io module, but I thought my from __future__ import absolute_import would have fixed that.

Thanks in advance for any help,

解决方案

That's the correct behaviour. If you want to fix the error simply do not run from inside the package.

When you run a script which is inside the package, python wont interpret that directory as a package, thus adding the working directory to the PYTHONPATH. That's why the io module imported by the zipfile module is your io module and not the one inside the standard library.

I'd recommend to create a simple launcher script outside your package (or in a bin/scripts folder), and launch that. This script can simply contain something like:

from pypackage1 import code

code.main()


An alternative to this is to tell the python interpreter that the file that you want to execute is part of a module. You can do this using the -m command line option. In your case you would have to do:

python -m pypackage1.code

Note that the argument of -m should be the module name, not the file name.

这篇关于在python中使用absolute_import和处理相对模块名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆