相对进口坏了吗? [英] relative import broken?

查看:79
本文介绍了相对进口坏了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相对进口是否在2.5?

目录``temp``包含::


__init__.py

test1.py

test2.py


文件内容:

__init__.py和test2.py是空的

test1.py包含一行::


来自。导入测试2

Win2000下的Python 2.5.1,cmd行执行,

产生输出::


回溯(最近一次调用最后一次):

文件F:\ temp \ test1.py,第1行,在< module>

来自。导入测试2

ValueError:在非包装中尝试相对导入


为什么?


谢谢,
Alan Isaac

Are relative imports broken in 2.5?
Directory ``temp`` contains::

__init__.py
test1.py
test2.py

File contents:
__init__.py and test2.py are empty
test1.py contains a single line::

from . import test2

Python 2.5.1 under Win2000, cmd line execution,
produces as output::

Traceback (most recent call last):
File "F:\temp\test1.py", line 1, in <module>
from . import test2
ValueError: Attempted relative import in non-package

Why?

Thanks,
Alan Isaac

推荐答案

Alan Isaac< ai **** @ american.eduwrote:
Alan Isaac <ai****@american.eduwrote:

相对进口是否在2.5?

目录``temp``包含::


__init __。py

test1.py

test2.py


文件内容:

__init__.py和test2.py是空的

test1.py包含一行::


来自。导入测试2

Win2000下的Python 2.5.1,cmd行执行,

产生输出::


回溯(最近一次调用最后一次):

文件F:\ temp \ test1.py,第1行,在< module>

来自。导入测试2

ValueError:尝试非包装中的相对导入


为什么?
Are relative imports broken in 2.5?
Directory ``temp`` contains::

__init__.py
test1.py
test2.py

File contents:
__init__.py and test2.py are empty
test1.py contains a single line::

from . import test2

Python 2.5.1 under Win2000, cmd line execution,
produces as output::

Traceback (most recent call last):
File "F:\temp\test1.py", line 1, in <module>
from . import test2
ValueError: Attempted relative import in non-package

Why?



如果您运行test1.py作为主模块,那么它不是

包的一部分,所以当你提到时,相对导入确实会失败。

OTOH,类似于:

If you''re running test1.py as your main module, then it''s not part of a
package, so the relative import should indeed fail as you mention.
OTOH, something like:


python -c''from temp import test1''

来自temp的父目录的
应该可以正常工作。既然你没有
给我们你想要执行的确切命令行,你就不可能猜到你正在做什么。

Alex
python -c''from temp import test1''

from the parent directory of temp should work fine. Since you don''t
give us the exact command line you''re trying to execute, it''s impossible
to guess exactly what you''re doing.
Alex


" Alex Martelli" < al *** @ mac.comwrote in message

news:1h ************************** @ mac.com ...
"Alex Martelli" <al***@mac.comwrote in message
news:1h**************************@mac.com...

如果您正在运行test1.py作为主模块,那么它不属于

包,所以当你提到时,相对导入确实会失败。
If you''re running test1.py as your main module, then it''s not part of a
package, so the relative import should indeed fail as you mention.



所以,如果我理解你,

在一个包中,我想要的任何模块

可用作为脚本执行

(以通常的方式,使用主要功能)

不能有任何相对进口。

这是对的吗?

这个限制的原因是什么? (它在哪里

有文件记录?)


谢谢,

Alan Isaac

So if I understand you,
in a package, any module that I wish
to make available for execution as a script
(in the usual way, with a main function)
cannot have any relative imports.
Is this right? What is the reason for
this restriction? (And where is it
documented?)

Thank you,
Alan Isaac


这篇关于相对进口坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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