简体   繁体   English

创建 python 脚本以使用源代码运行终端命令

[英]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.长话短说,我试图找出一种方法将这些命令行转换为脚本或 python 中的 function 可以由另一个 python 应用程序调用。

These are the command lines in Linux:这些是 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.起初我觉得这很容易,只需在 python 中启动应用程序 - 但我不确定如何处理源部分。 I thought it was just accessing the directory, but its doing something with the.csh file...我以为它只是在访问目录,但它对 .csh 文件做了一些事情......

I tried a very convoluted method by creating a.sh file like so:我通过创建一个像这样的 .sh 文件尝试了一种非常复杂的方法:

#!/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:然后为我的主程序制作一个 function 来调用所述文件:

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!任何建议将不胜感激!

I changed the function to:我将 function 更改为:

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.我使用 subprocess 而不是 os.system 并包含完整的文件路径,它现在可以工作了。 I think maybe it only needed the full file path, even though it was all in the same directory.我想也许它只需要完整的文件路径,即使它都在同一个目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM