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.
asked Apr 19, 2014 at 18:00
WyldStallyns
2011 gold badge6 silver badges19 bronze badges
2 Answers 2
Specify python and script.py as separated items:
call(['python', 'script.py'])
answered Apr 19, 2014 at 18:01
falsetru
371k69 gold badges770 silver badges660 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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?
answered Apr 19, 2014 at 23:43
jfs
417k211 gold badges1k silver badges1.7k bronze badges
Comments
lang-py
script.pyinside functions and useimport script; value = script.some_function(a, b)instead of running it as a subprocess?