How to incorporate environment from with python.

DL Neil PythonList at DancesWithMice.info
Sun Mar 22 12:24:32 EDT 2020


On 23/03/20 4:04 AM, Antoon Pardon wrote:
> I think I can best explain what I want by showing two bash sessions:
> 
> Session 1)
> ----------
> 
> $ /opt/csw/bin/python
> Python 2.6.4 (r264:75706, Sep  9 2015, 15:05:38) [C] on sunos5
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import cx_Oracle
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> ImportError: No module named cx_Oracle
>>>> ^D
> 
> ===================================
> 
> Session 2)
> ----------
> $ ORACLE_OWNER=...
> $ LD_LIBRARY_PATH=...
> $ ORACLE_SID=...
> $ TNS_ADMIN=...
> $ LD_RUN_PATH=...
> $ ORA_NLS33=...
> 
> $ export export LDFLAGS ORACLE_OWNER LD_LIBRARY_PATH ORACLE_SID TNS_ADMIN LD_RUN_PATH ORA_NLS33
> 
> $ /opt/csw/bin/python
> Python 2.6.7 (r267:88850, Feb 10 2012, 01:39:24) [C] on sunos5
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import cx_Oracle
>>>>
> 
> ========================================================
> 
> As you can see the import works in the second session because I
> had done the needed assignments and exports in bash.
> 
> Now I was wondering, if I could do those kind of preparations in python.
> I would like to start python from an unprepared bash, and do the necessary
> stuff to make the import work.
> 
> I already tried changing os.environ and using os.putenv, but that didn't
> seem to work.


It is possible to trap the import error using try...except.

The 'traditional' way to interrogate/modify the OS environment used 
https://docs.python.org/3/library/os.html#os.system

However, these days most prefer 
https://docs.python.org/3/library/subprocess.html

Please read the pros-and-cons carefully!
-- 
Regards =dn


More information about the Python-list mailing list