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.

1
  • 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? Commented Apr 19, 2014 at 23:30

2 Answers 2

1

Specify python and script.py as separated items:

call(['python', 'script.py'])
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.