1

I am now using Python to run a command-line executable program that was developed by myself:

import subprocess
cmd = '../../run_demo'
print cmd
subprocess.Popen(cmd)

This script runs very well in windows. However, it runs on linux, the following errors are given:

Traceback:
  File "script.py", line 6, in <module>
     subprocess.Popen(cmd)
  File "/user/lib/python2.5/subprocess.py", line 623, in _init_
     erread, errwrite)
  File "/user/lib/python2.6/subprocess.py", line 1141, in _execute_child
     raise child_exception
  OSError: [Errno 2] No such file or directory

As the executable command is printed in the script print cmd, if I copy the content of cmd, and then run in the command line, then the executable program can run. Any ideas? Thanks.

5
  • Is run_demo a script file ?? in which case the command would be cmd = '../.././run_demo' Commented Mar 13, 2014 at 11:58
  • 2
    @DOOM you don't need the ./ Commented Mar 13, 2014 at 11:59
  • What's your working directory when you run this? Does ../../run_demo work from the shell when you're in that working directory? Commented Mar 13, 2014 at 12:02
  • 1
    Take a look at Popen with string as commands Commented Mar 13, 2014 at 12:02
  • @DOOM Thanks for comments. run_demo is an executable program (written in c++ and compiled with g++) Commented Mar 13, 2014 at 12:02

1 Answer 1

2

well, as the error says:

OSError: [Errno 2] No such file or directory

there's no file such as '../../run_demo' in the path you're giving. My bet is that you're trying to call a script relative to the script's path whereas, it's relative to the path you're running it from.

So first thing, print what you've got at ../..:

import os
print os.listdir('../../')

there you'll see if there's a run_demo within.

Then, print the path of the current script:

pwd = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

and now try to play with the relative path to get from pwd to the run_demo path, for example:

rundemo_exec = os.path.join(pwd,'..','..','run_demo')

and finally, once you've verified all that, you want to call properly Popen:

subprocess.Popen([rundemo_exec])

or

subprocess.Popen(rundemo_exec, shell=True)

depending on whether you want to embed it in a shell or not.

N.B.: however the script is indeed or not in the path you're giving, you say you're making a "portable" application between linux and windows, so definitely you need to use os.path.join().

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

Comments

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.