首页 > 解决方案 > How to set an environment variable dependent on another one? (Windows)

问题描述

I am looking for a way on Windows how to set an environment variable dependent on another one. In my case, I want to add the new path to PYTHONPATH. Let's say, that there is an existing environment variable

%INSTALLATION_DIR% = D:\Programs\MyProject

The easiest way to do that would be:

SETX PYTHONPATH "%PYTHONPATH%;%INSTALLATION_DIR%\Utility\Scripts"

But then, %INSTALLATION_DIR% is directly replaced by D:\Programs\MyProject, so PYTHONPATH is not updated if %INSTALLATION_DIR% changes.

Is there a way to write the text %INSTALLATION_DIR% into an environment variable, without evaluating the variable directly?

If possible, I want to do that in an automated way (so using the console, powershell or python), as a want to write a script which adds a list of paths to PYTHONPATH.

标签: windowsenvironment-variableswindows-console

解决方案


我刚刚找到了解决方案。如果环境变量的名称写在引号中,则不会对其进行评估。

SETX PYTHONPATH "%PYTHONPATH%;%"INSTALLATION_DIR"%\Utility\Scripts"


推荐阅读