“导出”的子流程模块错误在Linux上的Python中?
我正在设置一个程序将我的计算机连接到我们学校的代理,目前有这样的内容:
import subprocess
import sys
username = 'fergus.barker'
password = '*************'
proxy = 'proxy.det.nsw.edu.au:8080'
options = '%s:%s@%s' % (username, password, proxy)
subprocess.Popen('export http_proxy=' + options)
但运行后我得到:
Traceback (most recent call last):
File "school_proxy_settings.py", line 19, in <module>
subprocess.Popen('export http_proxy=' + options)
File "/usr/lib/python2.6/subprocess.py", line 621, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
为什么会发生这种情况?
I'm setting up a program to connect my computer to our schools proxy and currently have something like this:
import subprocess
import sys
username = 'fergus.barker'
password = '*************'
proxy = 'proxy.det.nsw.edu.au:8080'
options = '%s:%s@%s' % (username, password, proxy)
subprocess.Popen('export http_proxy=' + options)
But upon running I get:
Traceback (most recent call last):
File "school_proxy_settings.py", line 19, in <module>
subprocess.Popen('export http_proxy=' + options)
File "/usr/lib/python2.6/subprocess.py", line 621, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Why is this happening please guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
export
不是实际的命令或文件。它是 bash 和 sh 等 shell 的内置命令,因此当您尝试 subprocess.Popen 时,您将收到异常,因为它找不到导出
命令。默认情况下,Popen
会执行os.execvp()
来生成新进程,这不允许您使用 shell 内部函数。您可以执行类似的操作,但必须更改对
Popen
的调用。http://docs.python.org/library/subprocess.html
您可以指定 < code>shell=True 使其使用 shell 命令。
class subprocess.Popen(args, bufsize=0,executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False,startupinfo=无,creationflags=0)
The problem is that
export
is not an actual command or file. It is a built-in command to shells likebash
andsh
, so when you attempt asubprocess.Popen
you will get an exception because it can not find theexport
command. By defaultPopen
does anos.execvp()
to spawn a new process, which would not allow you to use shell intrinsics.You can do something like this, though you have to change your call to
Popen
.http://docs.python.org/library/subprocess.html
You can specify
shell=True
to make it use shell commands.class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
export
不是系统上的单独二进制文件,它实际上只是 shell 本身内的一个命令。例如,尝试在您的系统上使用which rm
。您可能会看到类似以下内容:现在尝试使用
which export
。您会得到类似的信息:因此默认情况下您实际上无法调用
export
流程/子流程。您可能需要查看os.putenv()
< /a> 和os.environ()
相反。export
is not a separate binary on your system, it is actually just a command within the shell itself. For example, try usingwhich rm
on your system. You'll probably see something like:Now try using
which export
. You'll get something like:So you can't actually invoke an
export
process/subprocess by default. You may want to look atos.putenv()
andos.environ()
instead.