3

i'm trying to get simple python script to call another script, just in order to understand better how it's working. The 'main' code goes like this:

#!/usr/bin/python
import subprocess
subprocess.call('kvadrat.py')

and the script it calls - kvadrat.py:

#!/usr/bin/python
def kvadriranje(x):
    kvadrat = x * x
    return kvadrat

print kvadriranje(5)

Called script works on its own, but when called through 'main' script error occurs:

Traceback (most recent call last):
  File "/Users/user/Desktop/Python/General Test.py", line 5, in <module>
    subprocess.call('kvadrat.py')
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 595, in __init__
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 1106, in _execute_child
OSError: [Errno 2] No such file or directory

Obviously something's wrong, but being a beginner don't see what.

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
Fora Mejl
  • 33
  • 1
  • 1
  • 3
  • possible duplicate of [using python subprocess call to invoke python script](http://stackoverflow.com/questions/7152340/using-python-subprocess-call-to-invoke-python-script) – mmmmmm Jul 30 '13 at 15:12
  • are both of the source files in the same directory? Looks like the error is saying it can't find 'kvadrat.py' – vik Jul 30 '13 at 15:14
  • I believe you need to add python to your call unless python is your default on opening .py files – sihrc Jul 30 '13 at 15:48
  • they are both in the same directory. even if i add python to the call same error occurs. – Fora Mejl Jul 31 '13 at 10:54

3 Answers3

3

you need to give it the full path to the script that you are trying to call, if you want to do this dynamically (and you're in the same directory), you can do:

import os    
full_path = os.path.abspath('kvadrat.py')
Cameron Sparr
  • 3,925
  • 2
  • 22
  • 31
3

Have you tried:

from subprocess import call
call(["python","kvadrat.py"]) #if in same directory, else get abs path

You should also check if your file is there:

import os
print os.path.exists('kvadrat.py')
sihrc
  • 2,728
  • 2
  • 22
  • 43
  • tried this one, works also. but why this works and subprocess.call doesn't? isn't that suppose to do the same. file is in the same directory and has exe rights. – Fora Mejl Jul 31 '13 at 11:01
  • Actually, it's the only solution worked for me on Mac OS X 10.10 – lenhhoxung Nov 02 '15 at 11:30
0

Subprocess.call requires that the file is executable and found in path. In unix systems, you can try to use subprocess.call(['./kvadrat.py']) to execute a kvadrat.py file in the current working directory and make sure the kvadrat.py has executable permissions on; or you can copy it to a directory in your PATH, such as /usr/local/bin - then it is executable from anywhere as you wanted.

Most of the time you do not want to run other python applications using subprocess but instead just importing them as modules, however...