SaiVamshi Dobbali SaiVamshi Dobbali - 1 year ago 32
Python Question

Running Python script through subprocess fails

I am trying to run from script
. Both of them are in same directory. When I run them from current directory it works fine, but if I run
from different directory it fails.

#!/usr/bin/env python
import subprocess
print "done"

#!/usr/bin/env python
def display():
print "displaying"


/bin/sh: ./ No such file or directory

Answer Source

Executing ./ in a terminal means "Look in the current working directory for a file named and run it." The working directory is the directory from where you run the command. This means that your python script cannot see if you run it from a different directory.

If you are sure that and always lie in the same directory, you could use __file__ and os.path.dirname to prefix it with the directory of

#!/usr/bin/env python
import os
import subprocess

current_dir = os.path.dirname(__file__)
filename = os.path.join(current_dir, "")
out = subprocess.Popen([filename], shell=True)