Skip to content
Advertisement

Create python script to run terminal command with source

Long story short, I’m trying to figure out a way to turn these command lines into a script or function in python that can be called by another python application.

These are the command lines in Linux:

source tflite1-env/bin/activate
python3 stream.py --modeldir=TFLite_model

At first I was like this will be easy its just launching the application in python – but I’m not really sure how to deal with the source part. I thought it was just accessing the directory, but its doing something with the .csh file…

I tried a very convoluted method by creating a .sh file like so:

#!/bin/bash
cd ..
cd tflite1 
source tflite1-env/bin/activate
python3 stream.py --modeldir=TFLite_model

and then making a function for my main program to call said file:

import os
def startCamera():
os.system('sh script.sh')

With this method I get an error about the source not being found.

I think the issue is I’m basically trying to call too many separate processes that are terminating each other or something? There’s got to be a better way. Any suggestions would be greatly appreciated!

Advertisement

Answer

I changed the function to:

import subprocess
def startTheCamera()
  subprocess.call("/home/pi/Desktop/launchCamera.sh")

I used subprocess instead of os.system and included the full file path and it works now. I think maybe it only needed the full file path, even though it was all in the same directory.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement