[英]How to add a folder to the Windows PATH with Python?
I want to add a folder to the Windows PATH environment variable with Python.我想用 Python 在 Windows PATH 环境变量中添加一个文件夹。 I tried these three code snippets but none worked:我尝试了这三个代码片段,但都没有奏效:
os.environ['PATH'] += ";C:\my\folder"
and和
sys.path.insert(0, os.path.abspath('C:\my\folder'))
and和
if sys.platform == 'win32':
sep = ';'
else:
sep = ':'
os.environ['PATH'] += sep + r'"C:\my\folder"'
The Windows command for permanently changing the path is 用于永久更改路径的Windows命令是
setx /M path "%path%;C:\my\folder"
You can execute arbitrary shell commands via Python with os.system
您可以使用os.system
通过Python执行任意的shell命令
import os
os.system('setx /M path "%path%;C:\my\folder"')
Note: 注意:
You need to run this with elevated privileges. 您需要使用提升的特权来运行它。
References: 参考文献:
你应该使用:
os.environ['PATH'] += R";C:\\my\\folder"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.