Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling python script from another script

Tags:

python

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.

like image 345
Fora Mejl Avatar asked Dec 10 '25 10:12

Fora Mejl


1 Answers

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')
like image 192
Cameron Sparr Avatar answered Dec 12 '25 00:12

Cameron Sparr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!