简体   繁体   中英

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.

pythonscript.py指定为分隔项:

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM