0

i have a .bat file which executes multiple set command to add environment variables, for further scripts to execute,

i tried below code which is not working

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

below is my bat file

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\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;"

Edit: below script is from opnevino toolkit samples

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)
# -----------------------------------------------------------------------------------------------------

as per suggestion in comments i tried setting os.environ as below and checked with 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"]) prints correct value what i set but error i got is no module named openvino, if i set these variabel from bat file directly in cmd and then execute my py file in same cmd, it works.

problem is i want to set physical path of openvino installation in environment variable so in the next line i can do "import openvino", and os.environ sets environment variable after python.exe process starts , so may be this is not possible, os environ is may be only for data variable type purpose. am i right??

Rawat
  • 461
  • 3
  • 6
  • 23
  • 1
    Not sure, but I think that if you set env vars from a python script, they will only take persist for the duration of the shell session that the python file runs in. – SiHa Jul 08 '20 at 09:18
  • @SiHa yes variables will used only for script execution – Rawat Jul 08 '20 at 09:25
  • 3
    `subprocess.call([r'C:\Users\User\Desktop\my_vars.bat'])` results in starting in background `%ComSpec% /c C:\Users\User\Desktop\my_vars.bat` which means running `cmd.exe` to execute the batch file and then terminate. The environment variables are set in this command process, but that does not effect the environment variables list of `python.exe` nor of `cmd.exe` used to run `python.exe`. See chapter __F)__ on my answer on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi Jul 08 '20 at 09:37
  • 1
    You haven't posted the rest of the batch file, the one which uses `%PYTHONPATH%`, but fails. It seems as if you should be using `sys.path.append('C:\\Program Files (x86)\\IntelSWTools\\openvino\\deployment_tools\\model_optimizer')`, if you're not trying to access the variable in the same batch file/cmd instance as you're defining it. – Compo Jul 08 '20 at 09:42
  • @Mofi so should i echo all variables and set them from python code like this os.environ["PYTHONPATH"] = "full_py_path" – Rawat Jul 08 '20 at 09:43
  • 1
    Yes, take a look on [How to set environment variables in Python?](https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python) and [Python documentation of module os](https://docs.python.org/3/library/os.html). But the defined environment variables are only defined for `python.exe`, not for `cmd.exe` which started `python.exe` to execute the Python script. A child process cannot modify the environment variables of a parent process and a parent process cannot modify the environment variables of an already running child process. – Mofi Jul 08 '20 at 09:53
  • hello @Mofi i tried setting os.environ but still no module named openvino – Rawat Jul 08 '20 at 12:29
  • 1
    `%PY_HOME%` in `PATH` is definitely not correct. This environment variable reference must be already expanded on replacing environment variable `PATH`. It would be better to first get value of `PATH` and then prepend the directory paths you need for OpenVINO before setting `PATH`. However, I recommend first restart Windows if not already done after installation of OpenVINO. Then open a __new__ command prompt window and run `set >"%UserProfile%\Deskop\VariablesStandard.txt"`. Next run the batch file to setup OpenVINO environment. Then run `set >"%UserProfile%\Deskop\VariablesOpenVINO.txt"`. – Mofi Jul 08 '20 at 16:07
  • Compare the two text files for example with `fc "%UserProfile%\Deskop\VariablesStandard.txt" "%UserProfile%\Deskop\VariablesOpenVINO.txt"` and look on the differences, i.e. which environment variables were added by the batch file and which environment variables were modified and how were they modified by the batch file. Then you know which environment variables to define/modify in Python script. Open one more new command prompt and run your Python script and add code to output all environment variables to a text file before importing `openvino.inference_engine`. – Mofi Jul 08 '20 at 16:12
  • There should be no difference between `"%UserProfile%\Deskop\VariablesOpenVINO.txt"` and the text file created by your Python script with all the environment variables output by the Python script before importing `openvino.inference_engine`. – Mofi Jul 08 '20 at 16:21
  • @Mofi yeah, thanks for pointing out PY_HOME was not set i changed that in past for some reason, but still facing the problem – Rawat Jul 08 '20 at 18:22
  • problem is i want to set physical path of openvino installation in environment variable so in the next line i can do "import openvino", and os.environ sets environment variable after python.exe process starts , so may be this is not possible, os environ is may be only for data variable type purpose. – Rawat Jul 08 '20 at 19:08

1 Answers1

1

I'm not sure if what you are trying to do is possible. However, there is a guide to setup the OpenVINO environment variables permanently on Windows 10. This way you don't have to call the setupvars.bat script every time. The guide has a list of the variables needed for OpenVINO and their path.

jgespino
  • 34
  • 3