波彭和蟒蛇
处理一些代码,从命令提示符运行它时出现错误...
NameError: name 'Popen' is not defined
但我导入了 import os
和 import sys
。
这是代码的一部分
exepath = os.path.join(EXE File location is here)
exepath = '"' + os.path.normpath(exepath) + '"'
cmd = [exepath, '-el', str(el), '-n', str(z)]
print 'The python program is running this command:'
print cmd
process = Popen(cmd, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
我错过了一些基本的东西吗? 我不会怀疑这一点。 谢谢!
Working on some code and I'm given the error when running it from the command prompt...
NameError: name 'Popen' is not defined
but I've imported both import os
and import sys
.
Here's part of the code
exepath = os.path.join(EXE File location is here)
exepath = '"' + os.path.normpath(exepath) + '"'
cmd = [exepath, '-el', str(el), '-n', str(z)]
print 'The python program is running this command:'
print cmd
process = Popen(cmd, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
Am I missing something elementary? I wouldn't doubt it. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你应该做:
you should do:
popen定义在subprocess模块中
或者:
Popen is defined in the subprocess module
Or:
当您导入模块时,该模块的成员不会成为全局命名空间的一部分:您仍然需要在它们前面加上
modulename.
前缀。 因此,您必须说或者,您可以使用
from module import names
语法将内容导入到全局命名空间中:When you import a module, the module's members don't become part of the global namespace: you still have to prefix them with
modulename.
. So, you have to sayAlternatively, you can use the
from module import names
syntax to import things into the global namespace:这看起来像来自子进程模块的 Popen (python >= 2.4)
This looks like Popen from the subprocess module (python >= 2.4)
如果您的导入如下所示:
那么您需要像这样引用操作系统中包含的内容:
如果您不想这样做,您可以将导入更改为如下所示:
不建议这样做,因为它可能会导致命名空间模糊(代码中的内容与其他地方导入的内容相冲突。)您也可以这样做:
这比 from os import * 更明确且更易于阅读
If your import looks like this:
Then you need to reference the things included in os like this:
If you dont want to do that, you can change your import to look like this:
Which is not recommended because it can lead to namespace ambiguities (things in your code conflicting with things imported elsewhere.) You could also just do:
Which is more explicit and easier to read than from os import *
如果您只是导入 os.popen() ,则应该使用 os.popen() 。
You should be using os.popen() if you simply import os.