波彭和蟒蛇

发布于 2024-07-24 08:45:16 字数 542 浏览 5 评论 0原文

处理一些代码,从命令提示符运行它时出现错误...

NameError: name 'Popen' is not defined

但我导入了 import osimport 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

绝對不後悔。 2024-07-31 08:45:16

你应该做:

import subprocess
subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
# etc.

you should do:

import subprocess
subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
# etc.
厌倦 2024-07-31 08:45:16

popen定义在subprocess模块​​中

import subprocess
...
subprocess.Popen(...)

或者:

from subprocess import Popen
Popen(...)

Popen is defined in the subprocess module

import subprocess
...
subprocess.Popen(...)

Or:

from subprocess import Popen
Popen(...)
花心好男孩 2024-07-31 08:45:16

当您导入模块时,该模块的成员不会成为全局命名空间的一部分:您仍然需要在它们前面加上 modulename. 前缀。 因此,您必须说

import os
process = os.popen(command, mode, bufsize)

或者,您可以使用 from module import names 语法将内容导入到全局命名空间中:

from os import popen    # Or, from os import * to import everything
process = popen(command, mode, bufsize)

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 say

import os
process = os.popen(command, mode, bufsize)

Alternatively, you can use the from module import names syntax to import things into the global namespace:

from os import popen    # Or, from os import * to import everything
process = popen(command, mode, bufsize)
瑕疵 2024-07-31 08:45:16

这看起来像来自子进程模块的 Popen (python >= 2.4)

from subprocess import Popen

This looks like Popen from the subprocess module (python >= 2.4)

from subprocess import Popen
菩提树下叶撕阳。 2024-07-31 08:45:16

如果您的导入如下所示:

import os

那么您需要像这样引用操作系统中包含的内容:

os.popen()

如果您不想这样做,您可以将导入更改为如下所示:

from os import *

不建议这样做,因为它可能会导致命名空间模糊(代码中的内容与其他地方导入的内容相冲突。)您也可以这样做:

from os import popen

这比 from os import * 更明确且更易于阅读

If your import looks like this:

import os

Then you need to reference the things included in os like this:

os.popen()

If you dont want to do that, you can change your import to look like this:

from os import *

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:

from os import popen

Which is more explicit and easier to read than from os import *

待"谢繁草 2024-07-31 08:45:16

如果您只是导入 os.popen() ,则应该使用 os.popen() 。

You should be using os.popen() if you simply import os.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文