来自/ import语句行为的意外 [英] unexpected from/import statement behaviour

查看:75
本文介绍了来自/ import语句行为的意外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我正在尝试捕获我在python

程序中使用的外部模块的stderr。我正在这样做

通过在我的模块中设置一个类来覆盖stderr文件对象

方法写入。

外部模块输出以这种方式来stderr:


来自sys import std err


.....


打印> stderr,有些文字


在我的模块中我使用


import sys

..... sys.stderr ... sys.stdout


好​​吧,只要我不在外部模块中更改那些来自/

进口报表只需


导入系统

.....


print> sys.stderr,有些文字


我无法捕获它的stderr当然我不想要

做了这种改变。

我一直都相信两种使用方式的等价性

导入声明

但很明显我错了:-(


请,某人c告诉我发生了什么事?


提前致谢!

Hi all !

I''m trying to capture stderr of an external module I use in my python
program. I''m doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:

from sys import std err

.....

print >stderr, "Some text"

While in my module I use

import sys

..... sys.stderr ... sys.stdout

Well, as long as I do not change in the external module those from/
import statements to just

import sys

.....

print >sys.stderr, "Some text"

I''m not able to capture its stderr and of course I would like not to
do this kind of change.
I''ve always been convinced of the equivalence of the two ways of using
the import statement
but it''s clear I''m wrong :-(

Please, someone can tell me what''s going on ?

Thanks in advance !

推荐答案

nisp schrieb:
nisp schrieb:

大家好!


我正在尝试捕获我在python中使用的外部模块的stderr

计划。我正在这样做

通过在我的模块中设置一个类来覆盖stderr文件对象

方法写入。

外部模块输出这样对待stderr:


来自sys import std err


....


打印> stderr,有些文字


在我的模块中我使用


import sys


..... sys.stderr ... sys.stdout


好​​吧,只要我不改变外部模块那些来自/

导入声明只需


导入系统

....


打印> sys.stderr,有些文字


我无法抓住它的stderr,当然我不想

做这种改变。

我一直都相信使用

导入声明两种方式的等价性

但是很明显我错了:-(


请有人告诉我发生了什么事?
Hi all !

I''m trying to capture stderr of an external module I use in my python
program. I''m doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:

from sys import std err

....

print >stderr, "Some text"

While in my module I use

import sys

..... sys.stderr ... sys.stdout

Well, as long as I do not change in the external module those from/
import statements to just

import sys

....

print >sys.stderr, "Some text"

I''m not able to capture its stderr and of course I would like not to
do this kind of change.
I''ve always been convinced of the equivalence of the two ways of using
the import statement
but it''s clear I''m wrong :-(

Please, someone can tell me what''s going on ?

http:/ /effbot.org/zone/import-confusion.htm


Diez

http://effbot.org/zone/import-confusion.htm

Diez


nisp< emanuele.nesp ... @ gmail.comwrote:
nisp <emanuele.nesp...@gmail.comwrote:

我一直都相信两种使用方式的等价性

导入陈述,但很明显我错了:-(
I''ve always been convinced of the equivalence of the two ways of using
the import statement but it''s clear I''m wrong :-(



Python 2.5.2(r252:60911,2008年7月31日,17:28:52)

[gCC 4.2.3(Ubuntu 4.2.3-2ubuntu7)] on linux2

输入help,copyright,credit等等。或许可证或欲获得更多信息。

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.


>>来自sys import stderr
import sys
sys.stderr是stderr
>>from sys import stderr
import sys
sys.stderr is stderr



True


行为,我找不到两者之间的差异。


你可以削减&粘贴一个不会产生

正确行为的最小例子,或者提一下你正在使用什么类型的操作系统?

True

Behaviourly, I''m finding no difference between the two either.

Could you cut & paste a minimal example that isn''t producing the
correct behaviour, and perhaps mention what type of OS you''re using?


alex23 schrieb:
alex23 schrieb:

nisp< emanuele.nesp ... @ gmail.comwrote:
nisp <emanuele.nesp...@gmail.comwrote:

> ;我一直都相信使用
导入语句的两种方式是等价的,但很明显我错了:-(
>I''ve always been convinced of the equivalence of the two ways of using
the import statement but it''s clear I''m wrong :-(



Python 2.5.2(r252:60911,2008年7月31日,17:28:52)

[GCC 4.2.3(Ubuntu 4.2.3-2ubuntu7)] on linux2

输入帮助,版权,信用或许可证以获取更多信息。


Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.


>>>来自sys import stderr
import sys
sys.stderr是stderr
>>>from sys import stderr
import sys
sys.stderr is stderr



True


行为,我发现没什么不同介于两者之间。


你可以削减&粘贴一个最小的例子,它不会产生

正确的行为,也许会提到你正在使用什么类型的操作系统?

True

Behaviourly, I''m finding no difference between the two either.

Could you cut & paste a minimal example that isn''t producing the
correct behaviour, and perhaps mention what type of OS you''re using?



你没看过帖子的这一部分:


"""

我不能抓住它的stderr,当然我不想这样的改变。

"""


他试图将sys.stderr设置为一个新流并捕获打印

语句,但由于他创建了一个本地别名而无法这样做。

Diez

You did not read this part of the post:

"""
I''m not able to capture its stderr and of course I would like not to
do this kind of change.
"""

He tries to set sys.stderr to a new stream and capture the print
statements, but fails to do so because he created a local alias.
Diez


这篇关于来自/ import语句行为的意外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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