Execute a BASH command in Python-- in the same process
Question: 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
    


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

