Search code examples
python-2.7os.systemshebang

Shebang command to call script from existing script - Python


I am running a python script on my raspberry pi, at the end of which I want to call a second python script in the same directory. I call it using the os.system() command as shown in the code snippet below but get import errors. I understand this is because the system interprets the script name as a shell command and needs to be told to run it using python, using the shebang line at the beginning of my second script.

#!/usr/bin/env python

However doing so does not solve the errors

Here is the ending snippet from the first script:

# Time to Predict E
  end3 = time.time()
  prediction_time = end3-start3
  print ("\nPrediction time: ", prediction_time, "seconds")

  i = i+1
  print (i) 



script = '/home/pi/piNN/exampleScript.py'
os.system('"' + script + '"')

and here is the beginning of my second script:

'#!usr/bin/env python'

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#from picamera import PiCamera


import argparse
import sys
import time

import numpy as np
import tensorflow as tf
import PIL.Image as Image

Any help is greatly appreciated :)


Solution

  • Since you have not posted the actual errors that you get when you run your code, this is my best guess. First, ensure that exampleScript.py is executable:

    chmod +x /home/pi/piNN/exampleScript.py
    

    Second, add a missing leading slash to the shebang in exampleScript.py, i.e. change

    '#!usr/bin/env python'
    

    to

    '#!/usr/bin/env python'