Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python export env variable in Linux shell

I tried to export path to system environment and what happens: Path is for BerkleyDB library (need for shelve Python library, without it I have an error:

File "/kplusnfs/script/ns/processes/current/kondor_processes.py", line 214, in load_processes_list_for_hostname
    data_file = shelve.open((os.path.join(config.path_to_script, config.shelve_database)))
  File "/usr/local/lib/python2.6/shelve.py", line 234, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/usr/local/lib/python2.6/shelve.py", line 218, in __init__
    Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
  File "/usr/local/lib/python2.6/anydbm.py", line 82, in open
    mod = __import__(result)
  File "/usr/local/lib/python2.6/dbhash.py", line 8, in <module>
    import bsddb
  File "/usr/local/lib/python2.6/bsddb/__init__.py", line 64, in <module>
    import _bsddb
ImportError: ld.so.1: python: fatal: libdb-4.7.so: open failed: No such file or directory

When I add export to .bashrc line:

export LD_LIBRARY_PATH="/kplusnfs/script/ns/BerkleyDB/lib"

And reload bash then library is loaded.

Library path contains files:

ll /kplusnfs/script/ns/BerkleyDB/lib
total 12936
-rwxrwxrwx   1 c310176  atwuser  1470380 Aug  2 03:19 libdb-4.7.a
-rwxrwxrwx   1 c310176  atwuser     1078 Aug  2 03:19 libdb-4.7.la
-rwxrwxrwx   1 c310176  atwuser  1211376 Aug  2 03:19 libdb-4.7.so
-rwxrwxrwx   1 c310176  atwuser  1211376 Aug  2 03:19 libdb-4.so
-rwxrwxrwx   1 c310176  atwuser  1470380 Aug  2 03:19 libdb.a
-rwxrwxrwx   1 c310176  atwuser  1211376 Aug  2 03:19 libdb.so

I tried to use in code:

0

import os

1

os.environ["LD_LIBRARY_PATH"] = "/kplusnfs/script/ns/BerkleyDB/lib/"

2

os.system('export LD_LIBRARY_PATH="/kplusnfs/script/ns/BerkleyDB/lib"')

3

os.putenv("LD_LIBRARY_PATH", "/kplusnfs/script/ns/BerkleyDB/lib/")

And each time it was not working. But option #2 + below code for reloading bash:

os.system('bash')

did helped. But run of script was ridiculous, because it was loading bash and leaving previous operations...

So my question is - is there possible way not only to set variable but also load library for my user without reloading bash?

Ps. I do not have root privileges, so I am unable to change anything within the system (including installing any Python libraries)

like image 701
Tomasz Wójcik Avatar asked Dec 24 '22 04:12

Tomasz Wójcik


1 Answers

You seem to misunderstand what environment variables are, and how they are stored.

Every process (an instance of a running program) has an area of memory known as the environment block, this is where environment variables live. You used the phrase "system environment" - there is no such thing.

When a shell starts it creates some environment variables in its environment block, any process can do that. Bash invents some itself and takes others from startup files like .bashrc (Windows uses registry entries).

When the shell runs another program it runs in a child process. Various parts of the parent process are copied to the child (known as inheritance). One of these parts is the environment block.

A child process can change its own environment block, but that only changes its own local copy, it does not affect the parent. Changes are only propagated to children of the child, never back to the parent.

So when you do:

os.system('export LD_LIBRARY_PATH="/kplusnfs/script/ns/BerkleyDB/lib"')

that runs a child process (a shell), sets an environment variable in that child process, then exits and that was a waste of time, because nothing persists from that child.

When you do:

os.environ["LD_LIBRARY_PATH"] = "/kplusnfs/script/ns/BerkleyDB/lib/"

that sets an environment variable in your python process, then run your kondor_processes.py from the same python process (use subprocess.run). If you don't then as soon as you exit python then all the environment changes are lost.

However, if that's all you are going to do in the python then you might as well write a shell script wrapper with an export statement to run kondor_processes.py. Better to put that into your user's start-up file (.bashrc or .bash_profile).

like image 163
cdarke Avatar answered Dec 25 '22 17:12

cdarke