如何在 Windows 中使用 shell 脚本永久设置环境变量
Posted
技术标签:
【中文标题】如何在 Windows 中使用 shell 脚本永久设置环境变量【英文标题】:How to set environment vars permanently with a shell script in Windows 【发布时间】:2019-11-04 04:53:05 【问题描述】:我有一个 .bat,用于在运行一些程序之前设置一些环境变量。
我想永久设置这些环境变量,但如果可能的话我不想手动设置。这里有捷径吗?有没有我可以设置永久添加到 PATH 的标志?
代码来自于 OpenVino 提供的 setupvars.bat:
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
【问题讨论】:
查看帮助setx /?
但是不要使用setx
来修改system或userPATH
分别使用它非常明智,否则你正在破坏系统或用户PATH
。我建议阅读Why are other folder paths also added to system PATH with SetX and not only the specified folder path? 和How to search and replace a string in environment variable PATH? 和Adding the current directory to Windows path permanently。
为了更好地理解我的意思,请查看昨天的this question,其中用户执行了很可能带有类似setx PATH "C:\Whatever Folder Path;%PATH%" /M
之类的错误编码批处理文件,现在替换了所有环境变量引用的结果通过扩展字符串,在 user 和 system PATH
中复制文件夹路径并截断 PATH
s,因为 setx
限制为 1024 个字符,这在许多文件夹中并不多system PATH
. 中的路径(无用或不需要)
【参考方案1】:
对于 OpenVINO,每次打开命令行时都需要运行 setupvars.sh 脚本。但是,您也可以将所需的所有变量添加到系统环境变量中。
在您的 Windows® 10 系统上,转到控制面板 > 系统和安全 > 系统 > 高级系统设置 > 环境变量。
有关所需变量及其值的列表,请参阅 this document。
【讨论】:
以上是关于如何在 Windows 中使用 shell 脚本永久设置环境变量的主要内容,如果未能解决你的问题,请参考以下文章