Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

learn if program is installed from Python

Name: Anonymous 2008-01-07 20:40

I just ran a small Python script of mine on a new computer and I got an error message that xsltproc isn't found. So I want to modify the script to test whether xsltproc is installed. (I should probably do this with several other scripts too.)

However, I don't know how to do this. How do I learn, from a Python script running on Linux, whether a program is installed?

Name: Anonymous 2008-01-07 20:59

NO EXCEPTIONS

Name: Anonymous 2008-01-07 21:00

NO EXCEPTIONS

Name: Anonymous 2008-01-07 21:37

Split $PATH by ':' and look in each dir.

Name: Anonymous 2008-01-07 21:51

>>1
You could always run `which xsltproc`, however that's done in Python. If it returns 1, it's not found, if 0 then all is well.

Name: Anonymous 2008-01-07 21:55

>>4 You mean split $PATH by os.pathsep and look in each dir.

In code,

import os
f = 'xsltproc'
for d in os.environ.get('PATH', os.defpath).split(os.pathsep):
    path = os.path.join(d, f)
    if os.path.exists(path):
        break
else:
    path = None
print path or 'Not found.'

Name: Anonymous 2008-01-07 22:37

>>6
Yes, that's a much better idea than doing it the right way (>>5).

Name: Anonymous 2008-01-08 3:31

>>7
1. >>5 didn't give any code.
2. 'which' is not portable to other operating systems.
3. I was answering >>1's question.

Name: Anonymous 2008-01-08 6:29

>>8
xsltproc is not portable to the SNES.

Name: Anonymous 2008-01-08 6:41

>>9
True. No harm in making the program search reusable though.

Name: Anonymous 2008-01-08 7:09

I think the real problem here is that you are trying to use XSLT for something.

Name: Anonymous 2008-01-08 7:39

>>11
XSLT
XML.

Name: Anonymous 2008-01-08 7:52

XSLT
XML.
ENTERPRISE

Name: Anonymous 2008-01-08 12:36

OP here. I only use XSLT to transform XHTML documents into HTML-compatible documents. I'm not a fan of XSLT.

Today I was looking through Python's docs and I learned of a much easier solution. I haven't spent much time with Python, so I never heard of the subprocess module.
http://docs.python.org/lib/module-subprocess.html

It throws an exception when a program isn't installed, which for me is a good solution. So I ended up just changing os.system(...) calls to subprocess.call(...) calls. Now the script stops executing when a command fails, instead of barging on like before.

Name: Anonymous 2008-01-08 13:19

Damn I <3 Python so much it's criminal.

Name: Anonymous 2008-01-08 13:22

Damn I <3 Haskell so much it's criminal.

Name: Anonymous 2008-01-08 14:22

>>16
Damn I <3 Scheme so much it's my other car.

Name: Anonymous 2009-03-18 3:17

I feel the need, the need for weed!

Marijuana MUST be legalized.

Don't change these.
Name: Email:
Entire Thread Thread List