创建python脚本以运行带有源代码的终端命令

问题描述 投票:0回答:1

长话短说,我正在尝试找到一种方法,可以将这些命令行转换为python中的脚本或函数,以供其他python应用程序调用。

这些是Linux中的命令行:

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

起初,我很容易用python启动应用程序-但我不确定如何处理源代码部分。我以为它只是在访问目录,但是它对.csh文件有作用...

我尝试通过创建.sh文件来尝试一种非常复杂的方法:

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

然后为我的主程序创建一个函数以调用所述文件:

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

使用此方法,我收到关于找不到源的错误。

我认为问题是,我基本上是想调用太多彼此终止或彼此终止的独立进程?必须有一个更好的方法。任何建议将不胜感激!

python linux bash tensorflow raspbian
1个回答
0
投票

我将功能更改为:

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

我使用子进程而不是os.system,并包含了完整的文件路径,并且现在可以使用。我认为也许它只需要完整的文件路径,即使它们都位于同一目录中。

© www.soinside.com 2019 - 2024. All rights reserved.