2

I have a problem launching an executable from python. I boiled it down to this:

#!/usr/bin/python
import os
py_path='/home/jdoe/python/1509/bin/python'
os.execl( py_path, '/home/jdoe/run.py' )

This fails:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
ImportError: No module named site

I tried adding PYTHONHOME to the script and got other errors, but my question is: why does the script fail when:

/home/jdoe/python/1509/bin/python /home/jdoe/run.py

works?

os.exec() uses the calling script environment, so it should be just the same. What am I missing here?

3
  • Maybe the subprocess module is a little lighter to handle. Commented Feb 21, 2017 at 13:24
  • 1
    @MKesper, indeed, but there is a distinct use case subprocess can't handle. If they're fork()ing off a new process to do this exec, they'd be better off with subprocess; but if they want that new interpreter to take over their existing process's PID, then os.exec* would be the right tool for the job. Commented Feb 21, 2017 at 13:25
  • Yes. This is a simple launch script for a large python program which requires a custom-compiled python. The launch script must run with the machine python in a number of different environments. It has been written to run on python2.4.3 for one of the machines! Anyway, we do want to use os.exec to run in the current process. Commented Feb 21, 2017 at 13:34

1 Answer 1

2

Correct Usage

os.execl(py_path, py_path, '/home/jdoe/run.py')

From the documentation:

os.execl(path, arg0, arg1, ...)¶

arg0 is the executable's view of the name it was invoked with. By putting your run.py path in that position, you were preventing it from being present in arg1 -- the first location where regular arguments are expected.

Reproducing in a shell

Your error could be recreated from a shell, as follows:

(exec -a /home/jdoe/run.py /home/jdoe/python/1509/bin/python)

...passing /home/jdoe/run.py as arg0 rather than arg1. By default, however, a shell will always pass the name of an executable it's invoking as arg0, thus letting that executable know the name with which it was called.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. That works. It is a mystery to me now how this script was working yesterday, but this certainly fxes the current behaviour. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.