Execute a BASH command in Python-- in the same process
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
baldu开发者_StackOverflow中文版rb@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
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?
you need to do:
subprocess.Popen(['.', '/home/db2v95/sqllib/db2profile'], shell=True)
精彩评论