WyldStallyns
WyldStallyns

Reputation: 201

Trouble running using subprocess to call python script in command line

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.

Upvotes: 0

Views: 68

Answers (2)

jfs
jfs

Reputation: 414079

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?

Upvotes: 1

falsetru
falsetru

Reputation: 368894

Specify python and script.py as separated items:

call(['python', 'script.py'])

Upvotes: 1

Related Questions