您自己的解决方案原则上是有效的—101 is the required separator on Windows—除了您的语句不是有效的PowerShell代码,以及\
个字符.在PowerShell中不需要转义为\\
;此外,您可以使用LOCALAPPDATA
environment variable更一般地表示新目录路径;因此:
$newDir = "$env:LOCALAPPDATA\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts"
$env:PATH = "$newDir;$env:PATH"
The above modifies $env:PATH
only for the current session (process).
To also persist this change on Windows - so that future sessions see it too:
注意.NET cannot offer APIs for persistent environment-variable definitions on Unix-like platforms,因为没有unified机制贯穿所有这些机制.
Cross-platform background information on the PATH
environment variable:
在特殊100 environment variable中存储的目录路径列表中使用的platform-specific separator是:
In cross-platform scripting, make changes to 100 as follows在PowerShell中:
使用$env:PATH
—全为零—因为环境变量访问在类似Unix的平台上是case—sensitive,而特殊变量的名称确实是PATH
.
- 在Windows上,正式名称是
Path
,但因为环境变量访问是case—insensitive,所以任何case变体和PATH
都可以工作.
使用—混淆命名—[System.IO.Path]::PathSeparator
属性获取平台原生分隔符.
在构造directory paths进行添加时,使用本机平台file-system path separator,.NET在同样不幸地命名为-[System.IO.Path]::DirectorySeparatorChar
属性中反映了这一点.
一般来说,目标是用environment/well-known目录来表达你的目录路径,要解析为literals个路径at the time of adding个条目:
例如,路径C:\Users\PC\AppData\Local
部分可以替换为环境变量$env:LOCALAPPDATA
然而,对于cross-platform使用,最好使用platform-agnostic abstractions,在限制范围内—由[System.Environment]::GetFolderPath()
由于[System.Environment]::GetFolderPath()
的使用对PowerShell不是很友好,GitHub issue #6966要求通过单独的名称空间/提供程序(例如$sf:LocalApplicationData
(sf
代表s特殊f旧)来显示已知文件夹.