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

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

问题描述

我要执行的命令。 /家庭/ db2v95 / SQLLIB / db2profile中之前,我可以导入ibm_db_dbi 在Python 2.6。

执行它之前,我进入Python的作品:

  baldurb @ gigur:〜$。 /家庭/ db2v95 / SQLLIB / db2profile中
baldurb @ gigur:〜$蟒蛇
Python的2.6.4(r264:75706,2009年12月7日,18点45分15秒)
[GCC 4.4.1] linux2的上
键入help,版权,信用或许可的详细信息。
>>>进口ibm_db_dbi
>>>

但使用Python中执行它使用os.system(/家/ db2v95 / SQLLIB / db2profile中) subprocess.Popen([ /家/ db2v95 / SQLLIB / db2profile中])导致错误。我在做什么错了?

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

 >回溯(最近通话最后一个):
>文件<文件>的.py,8号线,在
> <&模块GT;
> subprocess.Popen([/家/ db2v95 / SQLLIB / db2profile中])
>文件
> /usr/lib/python2.6/subprocess.py
>线621,在__init__
> errread,ERRWRITE)文件/usr/lib/python2.6/subprocess.py
>线1126,在_execute_child
>提高child_exception OSERROR:[错误2]没有这样的文件或目录


解决方案

您调用'。 shell命令。这个命令的意思是在当前进程执行这个shell文件。因为Python是不是一个shell脚本间preTER你不能执行Python中的过程shell文件。

/家庭/ b2v95 / SQLLIB / db2profile中可能设置一些shell环境变量。如果你读它使用系统()函数,那么变量将只在执行的外壳改变,也不会在这一过程中调用该shell(脚本)可见。

您只能在开始你的Python脚本之前加载此文件 - 你可以做一个外壳包裹脚本,会做。 /家庭/ b2v95 / SQLLIB / db2profile中并执行Python脚本。

其他的方法是,看看 db2profile中包含。如果仅的name = value 行,你可以在你的python脚本和更新 os.environ 与解析数据获得的。如果脚本做更多的东西(比如调用别的东西来获取值),你可以在Python重新实现整个脚本。

更新一个想法:读剧本到Python,用管道(用popen)的外壳,剧本写 ENV 命令后,相同的外壳和读取输出。这样,您将获得在shell中定义的所有变量。现在,你可以阅读的变量。

事情是这样的:

  SHELL = subprocess.Popen([SH],标准输入= subprocess.PIPE,标​​准输出= subprocess.PIPE)
脚本=打开(/家庭/ db2v95 / SQLLIB / db2profile中,R)。阅读()
shell.stdin.write(脚本+\\ n)
shell.stdin.write(ENV \\ n)
shell.stdin.close()
在shell.stdout行:
    名称,值= line.strip()。分裂(=,1)
    os.environ [名] =价值

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

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

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?

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

解决方案

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.

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

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.

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.

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.

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

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

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