bash 在Python中执行BASH命令--在同一个进程中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2858920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:06:42  来源:igfitidea点击:

Execute a BASH command in Python-- in the same process

pythonbashdb2subprocess

提问by Iceland_Hyman

I need to execute the command . /home/db2v95/sqllib/db2profilebefore I can import ibm_db_dbiin Python 2.6.

我需要先执行命令,. /home/db2v95/sqllib/db2profile然后才能import ibm_db_dbi在 Python 2.6 中执行。

Executing it before I enter Python works:

在我进入 Python 之前执行它:

baldurb@gigur:~$ . /home/db2v95/sqllib/db2profile
baldurb@gigur:~$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ibm_db_dbi
>>> 

but executing it in Python using os.system(". /home/db2v95/sqllib/db2profile")or subprocess.Popen([". /home/db2v95/sqllib/db2profile"])results in an error. What am I doing wrong?

但是在 Python 中使用os.system(". /home/db2v95/sqllib/db2profile")or执行它会subprocess.Popen([". /home/db2v95/sqllib/db2profile"])导致错误。我究竟做错了什么?

Edit:this is the error I receive:

编辑:这是我收到的错误:

> Traceback (most recent call last):  
> File "<file>.py", line 8, in
> <module>
>     subprocess.Popen([". /home/db2v95/sqllib/db2profile"])  
> 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

回答by Jacek Konieczny

You are calling a '.' shell command. This command means 'execute this shell file in current process'. You cannot execute shell file in Python process as Python is not a shell script interpreter.

您正在调用“。” 外壳命令。这个命令的意思是“在当前进程中执行这个 shell 文件”。您不能在 Python 进程中执行 shell 文件,因为 Python 不是 shell 脚本解释器。

The /home/b2v95/sqllib/db2profileprobably sets some shell environment variables. If you read it using system()function, then the variables will be only changed in the shell executed and will not be visible in the process calling that shell (your script).

/home/b2v95/sqllib/db2profile可能设置一些shell环境变量。如果您使用system()函数读取它,那么变量将仅在执行的 shell 中更改,并且在调用该 shell(您的脚本)的进程中不可见。

You can only load this file before starting your python script – you could make a shell wrapper script which would do . /home/b2v95/sqllib/db2profileand execute your python script.

你只能在启动你的 python 脚本之前加载这个文件——你可以制作一个 shell 包装器脚本来执行. /home/b2v95/sqllib/db2profile和执行你的 python 脚本。

Other way would be to see what the db2profilecontains. If that are only NAME=valuelines, you could parse it in your python script and update os.environwith the data obtained. If script does something more (like calling something else to obtain the values) you can reimplement whole script in Python.

另一种方法是查看其中db2profile包含的内容。如果那只是NAME=value几行,您可以在您的 python 脚本中解析它并os.environ使用获得的数据进行更新。如果脚本执行更多操作(例如调用其他内容来获取值),您可以在 Python 中重新实现整个脚本。

UpdateAn idea: read the script into python, pipe it (using Popen) to the shell, after the script write envcommand to the same shell and read the output. This way you will get all the variables defined in the shell. Now you can read the variables.

更新一个想法:将脚本读入python,将它(使用Popen)通过管道传输到shell,在脚本写入env命令到同一个shell并读取输出之后。通过这种方式,您将获得 shell 中定义的所有变量。现在您可以读取变量。

Something like this:

像这样的东西:

shell = subprocess.Popen(["sh"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
script = open("/home/db2v95/sqllib/db2profile", "r").read()
shell.stdin.write(script + "\n")
shell.stdin.write("env\n")
shell.stdin.close()
for line in shell.stdout:
    name, value = line.strip().split("=", 1)
    os.environ[name] = value

回答by Peter Schuetze

Not sure what OS you are working on and what DB2 version you are using. The newer versions (at least 9.5 and above, not sure about 9.0 or 9.1) work by setting db2clp to **$$**. Since the DB2 usually is a LUW it might work under linux/unix too. However, under AIX I need to run the profile script to connect to the right DB instance. Haven't checked too much into what that script does.

不确定您正在使用什么操作系统以及您使用的是什么 DB2 版本。较新的版本(至少 9.5 及更高版本,不确定 9.0 或 9.1)通过将 db2clp 设置为**$$**. 由于 DB2 通常是 LUW,因此它也可以在 linux/unix 下工作。但是,在 AIX 下,我需要运行配置文件脚本才能连接到正确的数据库实例。还没有过多地检查该脚本的作用。

回答by SilentGhost

you need to do:

你需要做:

subprocess.Popen(['.', '/home/db2v95/sqllib/db2profile'], shell=True)

回答by Felix

Maybe os.popenis what you're looking for (better yet, one of the popen[2-4]variants)? Example:

也许os.popen是您正在寻找的(更好的是,其中一种popen[2-4]变体)?例子:

import os
p = os.popen(". /home/b2v95/sqllib/db2profile")
p.close() # this will wait for the command to finish
import ibm_db_dbi


Edit:I see that your error says No such file or directory. Try running it without the dot, like so:

编辑:我看到你的错误说No such file or directory。尝试在没有点的情况下运行它,如下所示:

os.popen("/home/b2v95/sqllib/db2profile")

If this doesn't work, it might have something to do with your environment. Maybe you're running Python jailed/chrooted?

如果这不起作用,则可能与您的环境有关。也许你正在运行 Python jailed/chrooted?