简体   繁体   English

如何在 Windows 中使用 shell 脚本永久设置环境变量

[英]How to set environment vars permanently with a shell script in Windows

I have a .bat I use to set some environment vars before running a few programs.我有一个 .bat,用于在运行一些程序之前设置一些环境变量。

I'd like to permanently set these environment vars, but I don't want to do it manually if possible.我想永久设置这些环境变量,但如果可能的话,我不想手动设置。 Is there a shortcut here?这里有捷径吗? Is there any flag I can set to permanently add to PATH ?是否有任何标志可以设置为永久添加到 PATH ?

The code is from setupvars.bat made available with OpenVino :代码来自 setupvars.bat 随 OpenVino 提供:

set ROOT=%~dp0
call :GetFullPath "%ROOT%\.." ROOT
set SCRIPT_NAME=%~nx0

set "INTEL_OPENVINO_DIR=%ROOT%"
set "INTEL_CVSDK_DIR=%INTEL_OPENVINO_DIR%"

where /q libmmd.dll || echo Warning: libmmd.dll couldn't be found in %%PATH%%. Please check if the redistributable package for Intel(R) C++ Compiler is installed and the library path is added to the PATH environment variable. System reboot can be required to update the system environment.

:: OpenCV
if exist "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat" (
call "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat"
) else (
set "OpenCV_DIR=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\lib"
set "PATH=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\bin;%PATH%"
)

:: OpenVX
set "OPENVX_FOLDER=%INTEL_OPENVINO_DIR%\openvx"
set "PATH=%INTEL_OPENVINO_DIR%\openvx\bin;%PATH%"

:: Inference Engine
set "InferenceEngine_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\share"
set "HDDL_INSTALL_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\hddl"
set "PATH=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Release;%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Debug;%HDDL_INSTALL_DIR%\bin;%PATH%"
if exist "%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions" (
set "ARCH_ROOT_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions"
)
:: Check if Python is installed
python --version 2>NUL
if errorlevel 1 (
   echo Error^: Python is not installed. Please install Python 3.5. or 3.6  ^(64-bit^) from https://www.python.org/downloads/
   exit /B 1
)

:: Check Python version
for /F "tokens=* USEBACKQ" %%F IN (`python --version 2^>^&1`) DO (
   set version=%%F
)
echo %var%

for /F "tokens=1,2,3 delims=. " %%a in ("%version%") do (
   set Major=%%b
   set Minor=%%c
)

if "%Major%" geq "3" (
   if "%Minor%" geq "5" (
      set python_ver=okay
   )
   if "%Minor%" geq "6" (
     set python_ver=okay
   )
)

if not "%python_ver%"=="okay" (
   echo Unsupported Python version. Please install Python 3.5 or 3.6  ^(64-bit^) from https://www.python.org/downloads/
   exit /B 1
)

:: Check Python bitness
python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2 > NUL
if errorlevel 1 (
   echo Error^: Error during installed Python bitness detection
   exit /B 1
)

for /F "tokens=* USEBACKQ" %%F IN (`python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2^>^&1`) DO (
   set bitness=%%F
)

if not "%bitness%"=="64" (
   echo Unsupported Python bitness. Please install Python 3.5 or 3.6  ^(64-bit^) from https://www.python.org/downloads/
   exit /B 1
)

set PYTHONPATH=%INTEL_OPENVINO_DIR%\python\python%Major%.%Minor%;%PYTHONPATH%

echo PYTHONPATH=%PYTHONPATH%

echo [setupvars.bat] OpenVINO environment initialized

exit /B 0

:GetFullPath
SET %2=%~f1

GOTO :EOF

For OpenVINO, you need to run the setupvars.sh script every time you open a command line.对于 OpenVINO,每次打开命令行时都需要运行 setupvars.sh 脚本。 However, you can also add all the variables needed to your systems environment variables.但是,您也可以将所需的所有变量添加到系统环境变量中。

On your Windows® 10 system, go to Control Panel > System and Security > System > Advanced System Settings > Environment Variables.在您的 Windows® 10 系统上,转至控制面板 > 系统和安全 > 系统 > 高级系统设置 > 环境变量。

See this document for a list of required variables and their values.有关所需变量及其值的列表,请参阅此文档

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

相关问题 如何永久设置 Windows 环境变量? - How do I set Windows Environment variables permanently? Power Shell的环境变量 - Environment vars by Power Shell 如何将perl脚本中设置的环境变量导出到批处理shell? - How to export environment variable set in perl script to batch shell? 在 Windows 中永久更改环境变量 - Permanently Change Environment Variables in Windows 如何从Tcl脚本设置Windows环境变量 - How to set Windows environment variable from Tcl script 如何为Visual Studio命令行永久设置环境变量 - How to permanently set Environment Variables for the Visual Studio command line 如何在Java中编写代码以运行在Windows环境中使用cygwin内部使用rSync的unix shell脚本? - How to code in java to run unix shell script which use rSync internally in windows environment using cygwin? 如何在Windows 10环境中从Java运行在Ubuntu上用Bash编写的Shell脚本? - How can I run a shell script, written in Bash on Ubuntu, from Java in a Windows 10 environment? 将环境变量设置为>'graterthan'Windows 7批处理脚本 - Set environment variable to > 'graterthan' windows 7 batch script 如何使用空环境创建Windows cmd外壳 - How to create a windows cmd shell with empty environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM