..... 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 pythonprogram. I''m doing thisby setting up a class in my module overwriting the stderr file objectmethod write.The external module outputs to stderr this way:from sys import std err.....print >stderr, "Some text"While in my module I useimport sys..... sys.stderr ... sys.stdoutWell, as long as I do not change in the external module those from/import statements to justimport sys.....print >sys.stderr, "Some text"I''m not able to capture its stderr and of course I would like not todo this kind of change.I''ve always been convinced of the equivalence of the two ways of usingthe import statementbut 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 pythonprogram. I''m doing thisby setting up a class in my module overwriting the stderr file objectmethod write.The external module outputs to stderr this way:from sys import std err....print >stderr, "Some text"While in my module I useimport sys..... sys.stderr ... sys.stdoutWell, as long as I do not change in the external module those from/import statements to justimport sys....print >sys.stderr, "Some text"I''m not able to capture its stderr and of course I would like not todo this kind of change.I''ve always been convinced of the equivalence of the two ways of usingthe import statementbut 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.htmDiez nisp< emanuele.nesp ... @ gmail.comwrote:nisp <emanuele.nesp...@gmail.comwrote: 我一直都相信两种使用方式的等价性 导入陈述,但很明显我错了:-(I''ve always been convinced of the equivalence of the two ways of usingthe 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 linux2Type "help", "copyright", "credits" or "license" for more information. >>来自sys import stderr import sys sys.stderr是stderr>>from sys import stderrimport syssys.stderr is stderr True 行为,我找不到两者之间的差异。 你可以削减&粘贴一个不会产生 正确行为的最小例子,或者提一下你正在使用什么类型的操作系统?TrueBehaviourly, I''m finding no difference between the two either.Could you cut & paste a minimal example that isn''t producing thecorrect 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 usingthe 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 linux2Type "help", "copyright", "credits" or "license" for more information. >>>来自sys import stderr import sys sys.stderr是stderr>>>from sys import stderrimport syssys.stderr is stderr True 行为,我发现没什么不同介于两者之间。 你可以削减&粘贴一个最小的例子,它不会产生 正确的行为,也许会提到你正在使用什么类型的操作系统?TrueBehaviourly, I''m finding no difference between the two either.Could you cut & paste a minimal example that isn''t producing thecorrect behaviour, and perhaps mention what type of OS you''re using? 你没看过帖子的这一部分: """ 我不能抓住它的stderr,当然我不想这样的改变。 """ 他试图将sys.stderr设置为一个新流并捕获打印 语句,但由于他创建了一个本地别名而无法这样做。 DiezYou did not read this part of the post:"""I''m not able to capture its stderr and of course I would like not todo this kind of change."""He tries to set sys.stderr to a new stream and capture the printstatements, but fails to do so because he created a local alias.Diez 这篇关于来自/ import语句行为的意外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!