Run the BASH command in Python - in the same process

I need to execute a command . /home/db2v95/sqllib/db2profilebefore I can import ibm_db_dbiin Python 2.6.

Doing this before entering Python:

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

> 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
+3
source share
4 answers

You call '.' shell. This command means "execute this shell file in the current process." You cannot execute a shell file in a Python process, because Python is not a shell script interpreter.

/home/b2v95/sqllib/db2profile, , . system(), , ( script).

python script - script, . /home/b2v95/sqllib/db2profile python script.

- , db2profile. NAME=value, python script os.environ . script - (, - ), script Python.

: script python, ( Popen) script write env , , . .

- :

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

:

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

, , DB2 . ( 9,5 , 9.0 9.1) , db2clp **$$**. DB2 LUW, linux/unix. AIX script, DB. , script.

0

, os.popen - , ( , popen[2-4])? :

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 what your error says No such file or directory. Try running it without a dot, for example:

os.popen("/home/b2v95/sqllib/db2profile")

If this does not work, it may have something to do with your environment. Maybe you use Python in jail / chrooted?

-1
source

Source: https://habr.com/ru/post/1746020/


All Articles