0

I'm trying to run a python script from another script using the following method:

from subprocess import call

call(['python script.py'])

but I'm getting the following error:

OSError: [Errno 2] No such file or directory

The files are both in the same directory. Help please.

WyldStallyns
  • 201
  • 1
  • 5
  • 19
  • Have you considered to put the code inside `script.py` inside functions and use `import script; value = script.some_function(a, b)` instead of running it as a subprocess? – jfs Apr 19 '14 at 23:30

2 Answers2

1

Specify python and script.py as separated items:

call(['python', 'script.py'])
falsetru
  • 357,413
  • 63
  • 732
  • 636
1

If the parent script is run from a different directory then you need a way to find where the script is stored:

#!/usr/bin/env python
import os
import sys
from subprocess import check_call

script_dir = os.path.dirname(sys.argv[0])
check_call([sys.executable or 'python', os.path.join(script_dir, 'script.py')])

See also How to properly determine current script directory in Python?

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670