python 运行 设置环境变量的bat文件

python run bat file to setup environment variables

我有一个 .bat 文件,它执行多个 set 命令来添加环境变量,以便执行更多脚本,

我尝试了以下无效的代码

import subprocess
subprocess.call([r'C:\Users\User\Desktop\my_vars.bat'])   

下面是我的bat文件

SET ROOT=C:\Program Files (x86)\IntelSWTools\openvino
set "INTEL_OPENVINO_DIR=C:\Program Files (x86)\IntelSWTools\openvino"
set "INTEL_CVSDK_DIR=C:\Program Files (x86)\IntelSWTools\openvino"
set "OpenCV_DIR=C:\Program Files (x86)\IntelSWTools\openvino\opencv\cmake"
set "InferenceEngine_DIR=C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\share"
set "HDDL_INSTALL_DIR=C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\external\hddl"
set "ngraph_DIR=C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\ngraph\cmake"
set PYTHONPATH=C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\open_model_zoo\tools\accuracy_checker;C:\Program Files (x86)\IntelSWTools\openvino\python\python3.7;C:\Program Files (x86)\IntelSWTools\openvino\python\python3;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\model_optimizer;
set "PATH=C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\ngraph\lib;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\external\tbb\bin;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\bin\intel64\Release;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\bin\intel64\Debug;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\external\hddl\bin;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\model_optimizer;C:\Program Files (x86)\IntelSWTools\openvino\opencv\bin;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;%PY_HOME%\Scripts;C:\Program Files (x86)\Windows Kits\Windows Performance Toolkit\;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Users\LaserTrac\Pictures\osm2pgsql-bin;C:\Program Files\PostgreSQL\bin;C:\mapnik-v2.2.0\lib;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\ms4w\Apache\cgi-bin;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Users\LaserTrac\Downloads\geckodriver-v0.26.0-win64;C:\Users\LaserTrac\Documents\ffmpeg\ffmpeg-20200209-5ad1c1a-win64-static\bin;C:\Program Files\PuTTY\;C:\apache-maven-3.6.3\bin;C:\Program Files\Google Protobuf\bin;C:\tensorflow1\models\research\object_detection;C:\tensorflow1\models\research\slim;C:\tensorflow1\models\research;C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Scripts;C:\ProgramData\Anaconda3\Library\bin;C:\Program Files\Java\jdk-13.0.2\bin;C:\ninja;C:\Users\LaserTrac\Downloads\sample\mingw\mingw32\bin;C:\msys64\usr\bin;C:\Program Files\CMake\bin;C:\Users\LaserTrac\AppData\Local\Microsoft\WindowsApps;C:\mapnik-v2.2.0\bin;C:\mapnik-v2.2.0\lib;C:\Users\LaserTrac\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\LaserTrac\AppData\Roaming\npm;C:\Program Files\Oracle\VirtualBox;"

编辑:以下脚本来自 opnevino 工具包示例

from __future__ import print_function
import subprocess
import os
#os.system("C:\Windows\System32\cmd.exe /c C:\Users\LaserTrac\Desktop\my_vars.bat")
#subprocess.call([r'C:\Program Files (x86)\IntelSWTools\openvino\bin\setupvars.bat'])
subprocess.call([r'C:\Users\LaserTrac\Desktop\my_vars.bat'])


#!/usr/bin/env python
  

import sys
import os
from argparse import ArgumentParser, SUPPRESS
import cv2
import numpy as np
import logging as log
from openvino.inference_engine import IECore
import datetime

#log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
log.info("Loading Inference Engine")
ie = IECore()

# --------------------------- 1. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
#model_xml = args.model
model_xml = "D:/openvino/2/frozen_inference_graph.xml"
model_bin = os.path.splitext(model_xml)[0] + ".bin"
#log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
net = ie.read_network(model=model_xml, weights=model_bin)
# -----------------------------------------------------------------------------------------------------

根据评论中的建议,我尝试如下设置 os.environ 并使用 print() 检查。

import os
#os.system("C:\Windows\System32\cmd.exe /c C:\Users\LaserTrac\Desktop\my_vars.bat")
#subprocess.call([r'C:\Program Files (x86)\IntelSWTools\openvino\bin\setupvars.bat'])
# subprocess.call([r'C:\Users\LaserTrac\Desktop\sample.bat'])
# import set_vars
os.environ["ROOT"] = "C:\Program Files (x86)\IntelSWTools\openvino"
os.environ["INTEL_OPENVINO_DIR"] = "C:\Program Files (x86)\IntelSWTools\openvino"
os.environ["INTEL_CVSDK_DIR"] = "C:\Program Files (x86)\IntelSWTools\openvino"
os.environ["OpenCV_DIR"] = "C:\Program Files (x86)\IntelSWTools\openvino\opencv\cmake"
os.environ["InferenceEngine_DIR"] = "C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\share"
os.environ["HDDL_INSTALL_DIR"] = "C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\external\hddl"
os.environ["ngraph_DIR"] = "C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\ngraph\cmake"
os.environ["PYTHONPATH"] = "C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\open_model_zoo\tools\accuracy_checker;C:\Program Files (x86)\IntelSWTools\openvino\python\python3.7;C:\Program Files (x86)\IntelSWTools\openvino\python\python3;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\model_optimizer"

os.environ["PATH"] = "C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\ngraph\lib;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\external\tbb\bin;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\bin\intel64\Release;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\bin\intel64\Debug;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\inference_engine\external\hddl\bin;C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\model_optimizer;C:\Program Files (x86)\IntelSWTools\openvino\opencv\bin;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;%PY_HOME%\Scripts;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Users\LaserTrac\Pictures\osm2pgsql-bin;C:\Program Files\PostgreSQL\10\bin;C:\mapnik-v2.2.0\lib;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\ms4w\Apache\cgi-bin;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Users\LaserTrac\Downloads\geckodriver-v0.26.0-win64;C:\Users\LaserTrac\Documents\ffmpeg\ffmpeg-20200209-5ad1c1a-win64-static\bin;C:\Program Files\PuTTY\;C:\apache-maven-3.6.3\bin;C:\Program Files\Google Protobuf\bin;C:\tensorflow1\models\research\object_detection;C:\tensorflow1\models\research\slim;C:\tensorflow1\models\research;C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Scripts;C:\ProgramData\Anaconda3\Library\bin;C:\Program Files\Java\jdk-13.0.2\bin;C:\ninja;C:\Users\LaserTrac\Downloads\sample\mingw\mingw32\bin;C:\msys64\usr\bin;C:\Program Files\CMake\bin;C:\Users\LaserTrac\AppData\Local\Microsoft\WindowsApps;C:\mapnik-v2.2.0\bin;C:\mapnik-v2.2.0\lib;C:\Users\LaserTrac\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\LaserTrac\AppData\Roaming\npm;C:\Program Files\Oracle\VirtualBox"

print(os.environ["PYTHONPATH"])
import from openvino.inference_engine import IECore

print(os.environ["PYTHONPATH"]) 打印我设置的正确值但是我得到的错误是没有名为 openvino 的模块,如果我直接在 cmd 中从 bat 文件设置这些变量然后执行我的 py文件在同一个cmd中,它有效。

问题是我想在环境变量中设置 openvino 安装的物理路径,所以在下一行我可以做“导入 openvino”,并且 os.environ 在 python.exe 进程启动后设置环境变量,所以可能这不是 possible,os environ 可能仅适用于数据变量类型 purpose。我说的对吗??

我不确定您尝试做的事情是否可行。但是,有一个设置 OpenVINO environment variables permanently on Windows 10 的指南。这样您就不必每次都调用 setupvars.bat 脚本。该指南列出了 OpenVINO 所需的变量及其路径。