Python 如何存储环境变量

Python 如何存储环境变量,python,windows,registry,environment-variables,Python,Windows,Registry,Environment Variables,要使用Windows命令处理器(cmd)设置环境变量,请执行以下操作: MY_变量现在可以由相同的cmd窗口启动的Python应用程序访问: import os variable = os.getenv('MY_VARIABLE') 我想知道是否有一种方法可以从Python内部设置一个环境变量,以便在同一台机器上运行的其他进程可以使用它? 要设置新的环境变量,请执行以下操作: os.environ['NEW_VARIABLE'] = 'NEW VALUE' 但是这个新的\u变量会在Pyth

要使用Windows命令处理器(
cmd
)设置环境变量,请执行以下操作:

MY_变量
现在可以由相同的
cmd
窗口启动的Python应用程序访问:

import os
variable = os.getenv('MY_VARIABLE') 
我想知道是否有一种方法可以从Python内部设置一个环境变量,以便在同一台机器上运行的其他进程可以使用它? 要设置新的环境变量,请执行以下操作:

os.environ['NEW_VARIABLE'] = 'NEW VALUE'

但是这个
新的\u变量
会在Python进程退出后立即丢失。

这样做的简单方法是使用os.system并传递命令,就像在CMD中运行它一样吗

例如,
os.system(“将我的变量设置为c:\path\to\filename.txt”)


希望这能有所帮助。`

一个简单的方法是使用os.system并像在CMD中运行命令一样传递命令吗

例如,
os.system(“将我的变量设置为c:\path\to\filename.txt”)


希望对您有所帮助。`

您可以在Windows注册表中持久存储环境变量。可以为当前用户或系统存储变量:

在Windows上持久设置环境变量的代码:

import win32con
import win32gui
try:
    import _winreg as winreg
except ImportError:
    # this has been renamed in python 3
    import winreg

def set_environment_variable(variable, value, user_env=True):
    if user_env:
        # This is for the user's environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_CURRENT_USER,
            'Environment', 0, winreg.KEY_SET_VALUE)
    else:
        # This is for the system environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_LOCAL_MACHINE,
            r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
            0, winreg.KEY_SET_VALUE)

    if '%' in value:
        var_type = winreg.REG_EXPAND_SZ
    else:
        var_type = winreg.REG_SZ
    with reg_key:
        winreg.SetValueEx(reg_key, variable, 0, var_type, value)

    # notify about environment change    
    win32gui.SendMessageTimeout(
        win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 
        'Environment', win32con.SMTO_ABORTIFHUNG, 1000)
set_environment_variable('NEW_VARIABLE', 'NEW VALUE')
上面要调用的测试代码:

import win32con
import win32gui
try:
    import _winreg as winreg
except ImportError:
    # this has been renamed in python 3
    import winreg

def set_environment_variable(variable, value, user_env=True):
    if user_env:
        # This is for the user's environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_CURRENT_USER,
            'Environment', 0, winreg.KEY_SET_VALUE)
    else:
        # This is for the system environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_LOCAL_MACHINE,
            r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
            0, winreg.KEY_SET_VALUE)

    if '%' in value:
        var_type = winreg.REG_EXPAND_SZ
    else:
        var_type = winreg.REG_SZ
    with reg_key:
        winreg.SetValueEx(reg_key, variable, 0, var_type, value)

    # notify about environment change    
    win32gui.SendMessageTimeout(
        win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 
        'Environment', win32con.SMTO_ABORTIFHUNG, 1000)
set_environment_variable('NEW_VARIABLE', 'NEW VALUE')

您可以在Windows注册表中持久存储环境变量。可以为当前用户或系统存储变量:

在Windows上持久设置环境变量的代码:

import win32con
import win32gui
try:
    import _winreg as winreg
except ImportError:
    # this has been renamed in python 3
    import winreg

def set_environment_variable(variable, value, user_env=True):
    if user_env:
        # This is for the user's environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_CURRENT_USER,
            'Environment', 0, winreg.KEY_SET_VALUE)
    else:
        # This is for the system environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_LOCAL_MACHINE,
            r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
            0, winreg.KEY_SET_VALUE)

    if '%' in value:
        var_type = winreg.REG_EXPAND_SZ
    else:
        var_type = winreg.REG_SZ
    with reg_key:
        winreg.SetValueEx(reg_key, variable, 0, var_type, value)

    # notify about environment change    
    win32gui.SendMessageTimeout(
        win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 
        'Environment', win32con.SMTO_ABORTIFHUNG, 1000)
set_environment_variable('NEW_VARIABLE', 'NEW VALUE')
上面要调用的测试代码:

import win32con
import win32gui
try:
    import _winreg as winreg
except ImportError:
    # this has been renamed in python 3
    import winreg

def set_environment_variable(variable, value, user_env=True):
    if user_env:
        # This is for the user's environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_CURRENT_USER,
            'Environment', 0, winreg.KEY_SET_VALUE)
    else:
        # This is for the system environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_LOCAL_MACHINE,
            r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
            0, winreg.KEY_SET_VALUE)

    if '%' in value:
        var_type = winreg.REG_EXPAND_SZ
    else:
        var_type = winreg.REG_SZ
    with reg_key:
        winreg.SetValueEx(reg_key, variable, 0, var_type, value)

    # notify about environment change    
    win32gui.SendMessageTimeout(
        win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 
        'Environment', win32con.SMTO_ABORTIFHUNG, 1000)
set_environment_variable('NEW_VARIABLE', 'NEW VALUE')

如果您的Python脚本启动了这些其他进程,那么这些进程将非常简单且可移植。如果没有父/子关系。。。好的,如果您需要的话,您可以将它们保存到Windows注册表中,以便在启动时为其他进程设置它们,但在这种情况下,您可能应该将您的问题标记为特定于Windows。当进程更新注册表中存储的环境变量时,它应该广播一个
WM\u SETTINGCHANGE
“环境”通知资源管理器重新加载其环境变量的消息。任何其他具有关注侦听的顶级窗口的进程在看到此消息时都可以执行相同的操作。诸如cmd.exe之类的控制台进程通常不拥有窗口(控制台窗口由conhost.exe拥有),因此它们通常不会收到此消息。如果您的Python脚本启动这些其他进程,则可以轻松地进行移植。如果没有父/子关系。。。好的,如果您需要的话,您可以将它们保存到Windows注册表中,以便在启动时为其他进程设置它们,但在这种情况下,您可能应该将您的问题标记为特定于Windows。当进程更新注册表中存储的环境变量时,它应该广播一个
WM\u SETTINGCHANGE
“环境”通知资源管理器重新加载其环境变量的消息。任何其他具有关注侦听的顶级窗口的进程在看到此消息时都可以执行相同的操作。诸如cmd.exe之类的控制台进程通常不拥有窗口(控制台窗口由conhost.exe拥有),因此它们通常不会收到此消息。这只会浪费时间。
SET
命令只影响立即退出的子进程。好的,好的。正如我在回答中所说的那样,当时我并不完全确定,也无法进行检查,但我认为这可能至少会将OP指向正确的方向。这只会浪费时间。
SET
命令只影响立即退出的子进程。好的,好的。正如我在回答中所说的那样,当时我并不完全确定,也无法进行检查,但我认为这可能至少会将OP指向正确的方向。