.pth files and figuring out valid paths..

R

rh0dium

I have a .pth file which has some logic in it - but it isn't quite
enough...

It started with this..
import os, site; site.addsitedir(os.path.join(os.environ["TECHROOT"],
"tools/python/modules"))

But that eventually evolved into..
import os, site; site.addsitedir(os.path.join(os.environ.get
("TECHROOT", "/home/tech"), "tools/python/modules"))

But now I want to check to make sure this directory exists or fall
back to "/home/tech". That was the point of the environ.get but what
if someone sets TECHROOT to /dev/null. Well that will break
things... I tried this but no go. Can someone help me out..

import os, site; smsc = os.environ.get("TECHROOT", "/home/tech"); if
not os.path.isdir(smsc): smsc = "/home/tech"; site.addsitedir
(os.path.join(smsc, "tools/python/Linux/%arch/lib/python2.5/site-
packages"))

Apparently there is a problem with the if statement???

Thanks
 
E

Emile van Sebille

On 6/9/2009 3:00 PM rh0dium said...
I have a .pth file which has some logic in it - but it isn't quite
enough...

It started with this..
import os, site; site.addsitedir(os.path.join(os.environ["TECHROOT"],
"tools/python/modules"))

But that eventually evolved into..
import os, site; site.addsitedir(os.path.join(os.environ.get
("TECHROOT", "/home/tech"), "tools/python/modules"))

But now I want to check to make sure this directory exists or fall
back to "/home/tech". That was the point of the environ.get but what
if someone sets TECHROOT to /dev/null. Well that will break
things... I tried this but no go. Can someone help me out..

You're not really putting all this on one line are you? If so, that's a
problem.
import os, site; smsc = os.environ.get("TECHROOT", "/home/tech"); if
not os.path.isdir(smsc): smsc = "/home/tech"; site.addsitedir
(os.path.join(smsc, "tools/python/Linux/%arch/lib/python2.5/site-
packages"))

Try it this way...

import os, site
smsc = os.environ.get("TECHROOT", "/home/tech")
if not os.path.isdir(smsc):
smsc = "/home/tech"
site.addsitedir (os.path.join(smsc,
"tools/python/Linux/%arch/lib/python2.5/site-packages"))

Emile
 
R

rh0dium

On 6/9/2009 3:00 PM rh0dium said...




I have a .pth file which has some logic in it - but it isn't quite
enough...
It started with this..
import os, site; site.addsitedir(os.path.join(os.environ["TECHROOT"],
"tools/python/modules"))
But that eventually evolved into..
import os, site; site.addsitedir(os.path.join(os.environ.get
("TECHROOT", "/home/tech"), "tools/python/modules"))
But now I want to check to make sure this directory exists or fall
back to "/home/tech".  That was the point of the environ.get but what
if someone sets TECHROOT to /dev/null.  Well that will break
things...  I tried this but no go.  Can someone help me out..

You're not really putting all this on one line are you?  If so, that's a
problem.
import os, site; smsc = os.environ.get("TECHROOT", "/home/tech"); if
not os.path.isdir(smsc): smsc = "/home/tech"; site.addsitedir
(os.path.join(smsc, "tools/python/Linux/%arch/lib/python2.5/site-
packages"))

Try it this way...

import os, site
smsc = os.environ.get("TECHROOT", "/home/tech")
if not os.path.isdir(smsc):
     smsc = "/home/tech"
site.addsitedir (os.path.join(smsc,
"tools/python/Linux/%arch/lib/python2.5/site-packages"))

Emile


Apparently there is a problem with the if statement???

No for .pth files this needs to be on a single line..
 
D

David Lyon

No for .pth files this needs to be on a single line..

I can't really see why you need conditional code...

If you want to add more locations...

Simply create another .PTH file.....

Having multiple paths or multiple .PTH files isn't a
problem for python.

David
 
A

alex23

Apparently there is a problem with the if statement???

Try restructuring the if as a ternary condition:

import os, site; smsc = os.environ.get("TECHROOT", "/home/tech"); smsc
= smsc if os.path.isdir(smsc) else "/home/tech"; site.addsitedir
(os.path.join(smsc, "tools/python/Linux/%arch/lib/python2.5/site-
packages"))
 
R

rh0dium

Try restructuring the if as a ternary condition:

import os, site; smsc = os.environ.get("TECHROOT", "/home/tech"); smsc
= smsc if os.path.isdir(smsc) else "/home/tech"; site.addsitedir
(os.path.join(smsc, "tools/python/Linux/%arch/lib/python2.5/site-
packages"))

Bingo - Nice Job!! Thanks! I had to look up ternary conditions!!
That's new for me (http://en.wikipedia.org/wiki/Ternary_operation)
 
R

rh0dium

I can't really see why you need conditional code...

If you want to add more locations...

Simply create another .PTH file.....

Having multiple paths or multiple .PTH files isn't a
problem for python.

David

We use it for our dev tree before we roll to production. Once dev is
QA'd then we (integrate) those changes to main and release.
 
D

David Lyon

..
We use it for our dev tree before we roll to production. Once dev is
QA'd then we (integrate) those changes to main and release.

Makes sense...

:)

I was just wondering...
 
G

Gabriel Genellina

On 6/9/2009 3:00 PM rh0dium said...
I have a .pth file which has some logic in it - but it isn't quite
enough...
It started with this..
import os, site; site.addsitedir(os.path.join(os.environ["TECHROOT"],
"tools/python/modules"))
But that eventually evolved into..
import os, site; site.addsitedir(os.path.join(os.environ.get
("TECHROOT", "/home/tech"), "tools/python/modules"))

Try it this way...

import os, site
smsc = os.environ.get("TECHROOT", "/home/tech")
if not os.path.isdir(smsc):
     smsc = "/home/tech"
site.addsitedir (os.path.join(smsc,
"tools/python/Linux/%arch/lib/python2.5/site-packages"))

No for .pth files this needs to be on a single line..

Try this instead:

import os, site; smsc = os.environ.get("TECHROOT", ""); smsc = smsc if
smsc and os.path.isdir(smsc) else "/home/tech"; site.addsitedir(...)

I'd add the directory unconditionally - then, when initializing the
application, I'd check that the required modules can be imported, and
inform the user of that specific problem if not.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top