在 Python 中执行 BASH 命令——在同一进程中 [英] Execute a BASH command in Python-- in the same process

查看:23
本文介绍了在 Python 中执行 BASH 命令——在同一进程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行命令./home/db2v95/sqllib/db2profile 在 Python 2.6 中 import ibm_db_dbi 之前.

I need to execute the command . /home/db2v95/sqllib/db2profile before I can import ibm_db_dbi in Python 2.6.

在我进入 Python 之前执行它是可行的:

Executing it before I enter Python works:

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
>>> 

但在 Python 中使用 os.system("./home/db2v95/sqllib/db2profile")subprocess.Popen(["./home/db2v95/sqllib/db2profile"]) 导致错误.我做错了什么?

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?

这是我收到的错误:

> 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

推荐答案

你调用的是'.'外壳命令.该命令的意思是在当前进程中执行这个 shell 文件".您不能在 Python 进程中执行 shell 文件,因为 Python 不是 shell 脚本解释器.

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.

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

The /home/b2v95/sqllib/db2profile probably 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).

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

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

另一种方法是查看 db2profile 包含的内容.如果这只是 NAME=value 行,您可以在您的 python 脚本中解析它并使用获得的数据更新 os.environ.如果脚本做了更多的事情(比如调用其他东西来获取值),你可以在 Python 中重新实现整个脚本.

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

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

Update An idea: read the script into python, pipe it (using Popen) to the shell, after the script write env command 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.

类似这样的:

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

这篇关于在 Python 中执行 BASH 命令——在同一进程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆