Interactive os.environ vs. os.environ in script

B

boris.smirnov

Hi there,

I have a problem with setting environment variable in my script that
uses qt library. For this library I have to define a path to tell the
script whre to find it.

I have a script called "shrink_bs_070226" that looks like this:
**********************************
import sys, re, glob, shutil
import os

os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'

from qt import *
***********************

When I run this script I get this error:

python shrink_bs_070226.py
Traceback (most recent call last):
File "shrink_bs_070226.py", line 25, in ?
from qt import *
File "/path/Linux/python/rh_linux/lib/python2.2/site-packages/
qt.py", line 46, in ?
import libsip
ImportError: libadamsqt.so: cannot open shared object file: No such
file or directory

What is important here that when I set this variable interactive in
python session, there is no problem with import.
Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "shrink_bs_070226.py", line 25, in ?
from qt import *
ImportError: No module named qt
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'
import shrink_bs_070226

Could anybody explain me the logic here? Am I missing something?
Thank in advance.
Rg
Boris
 
P

Peter Otten

Hi there,

I have a problem with setting environment variable in my script that
uses qt library. For this library I have to define a path to tell the
script whre to find it.

I have a script called "shrink_bs_070226" that looks like this:
**********************************
import sys, re, glob, shutil
import os

os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'

from qt import *
***********************

When I run this script I get this error:

python shrink_bs_070226.py
Traceback (most recent call last):
File "shrink_bs_070226.py", line 25, in ?
from qt import *
File "/path/Linux/python/rh_linux/lib/python2.2/site-packages/
qt.py", line 46, in ?
import libsip
ImportError: libadamsqt.so: cannot open shared object file: No such
file or directory

What is important here that when I set this variable interactive in
python session, there is no problem with import.
Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "shrink_bs_070226.py", line 25, in ?
from qt import *
ImportError: No module named qt
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'
import shrink_bs_070226

Could anybody explain me the logic here? Am I missing something?

Until Python 2.4 a failed import could leave some debris which would make
you think a second import did succeed.

Try
import os
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'
import shrink_bs_070226 # I expect that to fail

in a fresh interpreter to verify that what you see is an artifact of your
test method.

Peter
 
B

boris.smirnov

Hi there,
I have a problem with setting environment variable in my script that
uses qt library. For this library I have to define a path to tell the
script whre to find it.
I have a script called "shrink_bs_070226" that looks like this:
**********************************
import sys, re, glob, shutil
import os
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'

from qt import *
***********************
When I run this script I get this error:
Traceback (most recent call last):
File "shrink_bs_070226.py", line 25, in ?
from qt import *
File "/path/Linux/python/rh_linux/lib/python2.2/site-packages/
qt.py", line 46, in ?
import libsip
ImportError: libadamsqt.so: cannot open shared object file: No such
file or directory
What is important here that when I set this variable interactive in
python session, there is no problem with import.
Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
import shrink_bs_070226
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "shrink_bs_070226.py", line 25, in ?
from qt import *
ImportError: No module named qt
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'
import shrink_bs_070226
Could anybody explain me the logic here? Am I missing something?

Until Python 2.4 a failed import could leave some debris which would make
you think a second import did succeed.

Try
import os
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'
import shrink_bs_070226 # I expect that to fail

in a fresh interpreter to verify that what you see is an artifact of your
test method.

Peter- Hide quoted text -

- Show quoted text -

You are right:
Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'
import shrink_bs_070226
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "shrink_bs_070226.py", line 25, in ?
from qt import *
ImportError: No module named qt
OK then I have to reformulate my question. :)

In my script I have a line with

os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'

but this line didn't work.
But when I set this environment variable in Linux shell it works. Here
is a small example.
python shrink_bs_070226.py
Traceback (most recent call last):
File "shrink_bs_070226.py", line 25, in ?
from qt import *
File "/path/Linux/python/rh_linux/lib/python2.2/site-packages/
qt.py", line 46, in ?
import libsip
ImportError: libadamsqt.so: cannot open shared object file: No such
file or directory
setenv LD_LIBRARY_PATH /path/Linux/rh_linux
python shrink_bs_070226.py

Starting Script "Shrinker" ....

Why it's not working in script with command

os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'

but in shell it works?

I hope it's understandable. :)
Thanks,
boris
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

OK then I have to reformulate my question. :)

In my script I have a line with

os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'

but this line didn't work. But when I set this environment variable
in Linux shell it works. Here is a small example.
python shrink_bs_070226.py
Traceback (most recent call last): File "shrink_bs_070226.py", line
25, in ? from qt import * File
"/path/Linux/python/rh_linux/lib/python2.2/site-packages/ qt.py",
line 46, in ? import libsip ImportError: libadamsqt.so: cannot open
shared object file: No such file or directory
ld-elf.so reads environment variables when it was loaded.
It never reads environment variables again!
That you setting environment in the process does not make link-editor
to re-read environment variable and take effect.

To resolve the problem, you can try to add the path to sys.path.

- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF4w6H1LDUVnWfY8gRAoI0AKCLikYsFU2N6aaOZFDd1L2KY8DjqACg3QQn
KsEEcrvpw1CktEkVCKe/ojk=
=EQG6
-----END PGP SIGNATURE-----
 
B

boris.smirnov

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1





OK then I have to reformulate my question. :)
In my script I have a line with
os.environ["LD_LIBRARY_PATH"]='/path/Linux/rh_linux'

but this line didn't work. But when I set this environment variable
in Linux shell it works. Here is a small example.
python shrink_bs_070226.py
Traceback (most recent call last): File "shrink_bs_070226.py", line
25, in ? from qt import * File
"/path/Linux/python/rh_linux/lib/python2.2/site-packages/ qt.py",
line 46, in ? import libsip ImportError: libadamsqt.so: cannot open
shared object file: No such file or directory

ld-elf.so reads environment variables when it was loaded.
It never reads environment variables again!
That you setting environment in the process does not make link-editor
to re-read environment variable and take effect.

To resolve the problem, you can try to add the path to sys.path.

- --
Thinker Li - (e-mail address removed) (e-mail address removed)://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iD8DBQFF4w6H1LDUVnWfY8gRAoI0AKCLikYsFU2N6aaOZFDd1L2KY8DjqACg3QQn
KsEEcrvpw1CktEkVCKe/ojk=
=EQG6
-----END PGP SIGNATURE------ Hide quoted text -

- Show quoted text -

Hi,
thanks for the tip, but it didn't help.

I used:
sys.path.append('/path/Linux/rh_linux')

and the problem still persists:

Traceback (most recent call last):
File "shrink_bs_070226.py", line 26, in ?
from qt import *
File "/path/Linux/python/rh_linux/lib/python2.2/site-packages/
qt.py", line 46, in ?
import libsip
ImportError: libadamsqt.so: cannot open shared object file: No such
file or directory

Is there another possibility of how to solve it just by adding some
lines in script?

Thanks
Boris
 
P

Peter Otten

Is there another possibility of how to solve it just by adding some
lines in script?

I think you have to wrap your script in a shell script

#!/bin/sh
export LD_LIBRARY_PATH=/path/Linux/rh_linux
python shrink_bs_070226.py

To avoid that you can have the python script invoke itself, e. g.:

#!/usr/bin/env python
import os
if os.environ.get("YADDA") != "whatever":
print "fix environment"
os.environ["YADDA"] = "whatever"
os.system("yadda.py")
raise SystemExit

print "YADDA is now %r" % os.environ["YADDA"]
print "do your real stuff"

Peter
 
B

boris.smirnov

Is there another possibility of how to solve it just by adding some
lines in script?

I think you have to wrap your script in a shell script

#!/bin/sh
export LD_LIBRARY_PATH=/path/Linux/rh_linux
python shrink_bs_070226.py

To avoid that you can have the python script invoke itself, e. g.:

#!/usr/bin/env python
import os
if os.environ.get("YADDA") != "whatever":
print "fix environment"
os.environ["YADDA"] = "whatever"
os.system("yadda.py")
raise SystemExit

print "YADDA is now %r" % os.environ["YADDA"]
print "do your real stuff"

Peter

Thanks for the reply.

If I that good understood I did this:
**********************************
import sys, re, glob, shutil
import os


if os.environ["LD_LIBRARY_PATH"].count('/path/Linux/rh_linux') !=1:
os.environ["LD_LIBRARY_PATH"]=os.environ["LD_LIBRARY_PATH"]+':'+'/
path/Linux/rh_linux'
os.system("shrink_bs_070226.py")
raise SystemExit

from qt import *
***********************

Is that correct. If yes then it not works. If not then, what's wrong?
python shrink_bs_070226.py

Traceback (most recent call last):
File "./shrink_bs_070226.py", line 29, in ?
from qt import *
ImportError: No module named qt

Thanks
Boris
 
P

Peter Otten

Is there another possibility of how to solve it just by adding some
lines in script?

I think you have to wrap your script in a shell script

#!/bin/sh
export LD_LIBRARY_PATH=/path/Linux/rh_linux
python shrink_bs_070226.py

To avoid that you can have the python script invoke itself, e. g.:

#!/usr/bin/env python
import os
if os.environ.get("YADDA") != "whatever":
print "fix environment"
os.environ["YADDA"] = "whatever"
os.system("yadda.py")
raise SystemExit

print "YADDA is now %r" % os.environ["YADDA"]
print "do your real stuff"

Peter

Thanks for the reply.

If I that good understood I did this:
**********************************
import sys, re, glob, shutil
import os


if os.environ["LD_LIBRARY_PATH"].count('/path/Linux/rh_linux') !=1:
os.environ["LD_LIBRARY_PATH"]=os.environ["LD_LIBRARY_PATH"]+':'+'/
path/Linux/rh_linux'
os.system("shrink_bs_070226.py")
raise SystemExit

from qt import *
***********************

Is that correct. If yes then it not works. If not then, what's wrong?

I don't know. You are not quoting the actual code you are using (from qt
import * is definitely not in line 29, and the #!/path/to/your/python is
missing). Do you have more than one python version installed? then you may
have to put

#!/usr/bin/python2.2

or similar in the first line of your script.

Peter
 
B

boris.smirnov

(e-mail address removed) wrote:
Is there another possibility of how to solve it just by adding some
lines in script?
I think you have to wrap your script in a shell script
#!/bin/sh
export LD_LIBRARY_PATH=/path/Linux/rh_linux
python shrink_bs_070226.py
To avoid that you can have the python script invoke itself, e. g.:
#!/usr/bin/env python
import os
if os.environ.get("YADDA") != "whatever":
print "fix environment"
os.environ["YADDA"] = "whatever"
os.system("yadda.py")
raise SystemExit
print "YADDA is now %r" % os.environ["YADDA"]
print "do your real stuff"
Peter
Thanks for the reply.
If I that good understood I did this:
**********************************
import sys, re, glob, shutil
import os
if os.environ["LD_LIBRARY_PATH"].count('/path/Linux/rh_linux') !=1:
os.environ["LD_LIBRARY_PATH"]=os.environ["LD_LIBRARY_PATH"]+':'+'/
path/Linux/rh_linux'
os.system("shrink_bs_070226.py")
raise SystemExit
from qt import *
***********************
Is that correct. If yes then it not works. If not then, what's wrong?

I don't know. You are not quoting the actual code you are using (from qt
import * is definitely not in line 29, and the #!/path/to/your/python is
missing). Do you have more than one python version installed? then you may
have to put

#!/usr/bin/python2.2

or similar in the first line of your script.

Peter- Hide quoted text -

- Show quoted text -

Probably I understood it not correctly. What did you mean was to put
that code into my python script "shrink_bs_070226.py" and then calls
script itself?

So I took my script "shrink_bs_070226.py" and it starts with:
**********************************
#!/usr/bin/env python
import sys, re, glob, shutil
import os

then I put your code in that script

if os.environ["LD_LIBRARY_PATH"].count('/path/Linux/rh_linux') !=1:
os.environ["LD_LIBRARY_PATH"]=os.environ["LD_LIBRARY_PATH"]+':'+'/
path/Linux/rh_linux'
os.system("shrink_bs_070226.py")
raise SystemExit

and here continues my script:

from qt import *

......
***********************

Is it correct?

BTW: On lines 3:18 I have comments, script description and
modifications, therefore is "from qt import *" on the line 29
 
P

Peter Otten

Probably I understood it not correctly. What did you mean was to put
that code into my python script "shrink_bs_070226.py" and then calls
script itself?

Yes.
So I took my script "shrink_bs_070226.py" and it starts with:
**********************************
#!/usr/bin/env python
import sys, re, glob, shutil
import os

then I put your code in that script

if os.environ["LD_LIBRARY_PATH"].count('/path/Linux/rh_linux') !=1:
os.environ["LD_LIBRARY_PATH"]=os.environ["LD_LIBRARY_PATH"]+':'+'/
path/Linux/rh_linux'
os.system("shrink_bs_070226.py")
raise SystemExit

and here continues my script:

from qt import *

.....
***********************

Is it correct?

Yes.
BTW: On lines 3:18 I have comments, script description and
modifications, therefore is "from qt import *" on the line 29

The problem must be something else, then.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top