且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:平***立的方式来修改PATH环境变量

更新时间:2023-10-10 14:16:46

您应该可以修改 os.environ



由于 os.pathsep 是分隔不同路径的字符,您应该使用它来附加每个新路径:

  os.environ [PATH] + = os.pathsep +路径

或者,如果列表中添加几条路径:

  os.environ [PATH] + = os.pathsep + os.pathsep.join(pathlist)

正如你所提到的,$ c> os.path.join 也可以用于每个单独的路径,你必须从单独的部分构造它们。 >

Is there a way to modify the PATH environment variable in a platform independent way using python?

Something similar to os.path.join()?

You should be able to modify os.environ.

Since os.pathsep is the character to separate different paths, you should use this to append each new path:

os.environ["PATH"] += os.pathsep + path

or, if there are several paths to add in a list:

os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)

As you mentioned, os.path.join can also be used for each individual path you have to append in the case you have to construct them from separate parts.