Sunday, February 24, 2013

Python subprocess throws [Errno 2] No such file or directory

First steps in Python, and I've been spending a while trying to understand why my python script would fail when called from my webserver. The same script would work just fine when I run it manually.

simple example

file  run_maven_clean.py:

        #!/usr/bin/python
        import os, sys,


        subprocess.check_call(["mvn", "clean", "install"])

This will work if run manually
user:~$  run_maven_clean.py
mvn clean install

but fails terribly when called by a web server

  File "/home/use/run_maven_clean.py", line 105, in build
    subprocess.check_call(cmd)
  File "/usr/lib/python2.5/subprocess.py", line 457, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib/python2.5/subprocess.py", line 444, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.5/subprocess.py", line 594, in __init__
    errread, errwrite)
  File "/usr/lib/python2.5/subprocess.py", line 1149, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory


Reading around, I found tips, that there is no shell, make sure you have bash or csh... Of course, I have shell=False for security, still, I thought "mvn" is pretty well known executable on the system, it should find it, well NO.

NO SHELL also means, you have NO PATH, NO ENVIRONMENT VARIABLES.

and the correct way to do really is

              subprocess.check_call(["/opt/apache-maven-3.0.4/bin/mvn", "clean", "install"])