Python:独立于平台的修改PATH环境变量的方法

时间:2022-01-01 11:39:57

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

有没有办法使用python以独立于平台的方式修改PATH环境变量?

Something similar to os.path.join()?

类似于os.path.join()的东西?

3 个解决方案

#1


120  

You should be able to modify os.environ.

您应该能够修改os.environ。

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

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

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.

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

#2


14  

Please note that os.environ is not actually a dictionary. It's a special dictionary-like object which actually sets environment variables in the current process using setenv.

请注意,os.environ实际上不是字典。它是一个特殊的类字典对象,实际上使用setenv在当前进程中设置环境变量。

>>> os.environ.__class__
<class os._Environ at 0x100472050>
>>> import os
>>> os.environ["HELLO"] = "WORLD"
>>> os.getenv("HELLO")
'WORLD'

This means that PATH (and other environment variables) will be visible to C code run in the same process.

这意味着在同一进程中运行的C代码可以看到PATH(和其他环境变量)。

(Since comments can't contain formatting, I have to put this in an answer, but I feel like it's an important point to make. This is really a comment on the comment about there being no equivalent to 'export'.)

(由于评论不能包含格式,我必须把它放在一个答案中,但我觉得这是一个重要的观点。这实际上是关于没有相当于'export'的评论的评论。)

#3


7  

The caveat to be aware of with modifying environment variables in Python, is that there is no equivalent of the "export" shell command. There is no way of injecting changes into the current process, only child processes.

需要注意的是在Python中修改环境变量,就是没有“export”shell命令的等价物。没有办法将更改注入当前进程,只有子进程。

#1


120  

You should be able to modify os.environ.

您应该能够修改os.environ。

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

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

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.

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

#2


14  

Please note that os.environ is not actually a dictionary. It's a special dictionary-like object which actually sets environment variables in the current process using setenv.

请注意,os.environ实际上不是字典。它是一个特殊的类字典对象,实际上使用setenv在当前进程中设置环境变量。

>>> os.environ.__class__
<class os._Environ at 0x100472050>
>>> import os
>>> os.environ["HELLO"] = "WORLD"
>>> os.getenv("HELLO")
'WORLD'

This means that PATH (and other environment variables) will be visible to C code run in the same process.

这意味着在同一进程中运行的C代码可以看到PATH(和其他环境变量)。

(Since comments can't contain formatting, I have to put this in an answer, but I feel like it's an important point to make. This is really a comment on the comment about there being no equivalent to 'export'.)

(由于评论不能包含格式,我必须把它放在一个答案中,但我觉得这是一个重要的观点。这实际上是关于没有相当于'export'的评论的评论。)

#3


7  

The caveat to be aware of with modifying environment variables in Python, is that there is no equivalent of the "export" shell command. There is no way of injecting changes into the current process, only child processes.

需要注意的是在Python中修改环境变量,就是没有“export”shell命令的等价物。没有办法将更改注入当前进程,只有子进程。