Execute a BASH command in Python— in the same process

吃可爱长大的小学妹 提交于 2019-12-05 18:09:45

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

you need to do:

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

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.

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

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:

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!