• <i id='fZXXM'><tr id='fZXXM'><dt id='fZXXM'><q id='fZXXM'><span id='fZXXM'><b id='fZXXM'><form id='fZXXM'><ins id='fZXXM'></ins><ul id='fZXXM'></ul><sub id='fZXXM'></sub></form><legend id='fZXXM'></legend><bdo id='fZXXM'><pre id='fZXXM'><center id='fZXXM'></center></pre></bdo></b><th id='fZXXM'></th></span></q></dt></tr></i><div id='fZXXM'><tfoot id='fZXXM'></tfoot><dl id='fZXXM'><fieldset id='fZXXM'></fieldset></dl></div>

    <small id='fZXXM'></small><noframes id='fZXXM'>

    1. <tfoot id='fZXXM'></tfoot>
        <bdo id='fZXXM'></bdo><ul id='fZXXM'></ul>
      <legend id='fZXXM'><style id='fZXXM'><dir id='fZXXM'><q id='fZXXM'></q></dir></style></legend>

        Python - 临时修改当前进程的环境

        Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)

          <small id='lHx3z'></small><noframes id='lHx3z'>

            <tbody id='lHx3z'></tbody>
            <legend id='lHx3z'><style id='lHx3z'><dir id='lHx3z'><q id='lHx3z'></q></dir></style></legend>

                • <bdo id='lHx3z'></bdo><ul id='lHx3z'></ul>
                • <i id='lHx3z'><tr id='lHx3z'><dt id='lHx3z'><q id='lHx3z'><span id='lHx3z'><b id='lHx3z'><form id='lHx3z'><ins id='lHx3z'></ins><ul id='lHx3z'></ul><sub id='lHx3z'></sub></form><legend id='lHx3z'></legend><bdo id='lHx3z'><pre id='lHx3z'><center id='lHx3z'></center></pre></bdo></b><th id='lHx3z'></th></span></q></dt></tr></i><div id='lHx3z'><tfoot id='lHx3z'></tfoot><dl id='lHx3z'><fieldset id='lHx3z'></fieldset></dl></div>
                • <tfoot id='lHx3z'></tfoot>
                • 本文介绍了Python - 临时修改当前进程的环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用以下代码临时修改环境变量.

                  I use the following code to temporarily modify environment variables.

                  @contextmanager
                  def _setenv(**mapping):
                      """``with`` context to temporarily modify the environment variables"""
                      backup_values = {}
                      backup_remove = set()
                      for key, value in mapping.items():
                          if key in os.environ:
                              backup_values[key] = os.environ[key]
                          else:
                              backup_remove.add(key)
                          os.environ[key] = value
                  
                      try:
                          yield
                      finally:
                          # restore old environment
                          for k, v in backup_values.items():
                              os.environ[k] = v
                          for k in backup_remove:
                              del os.environ[k]
                  

                  这个with上下文主要用在测试用例中.例如,

                  This with context is mainly used in test cases. For example,

                  def test_myapp_respects_this_envvar():
                      with _setenv(MYAPP_PLUGINS_DIR='testsandbox/plugins'):
                          myapp.plugins.register()
                          [...]
                  

                  我的问题:有没有一种简单/优雅的方式来编写 _setenv?我考虑过实际执行 backup = os.environ.copy() 然后 os.environ = backup .. 但我不确定这是否会影响程序行为(例如:如果 os.environ 在 Python 解释器的其他地方被引用).

                  My question: is there a simple/elegant way to write _setenv? I thought about actually doing backup = os.environ.copy() and then os.environ = backup .. but I am not sure if that would affect the program behavior (eg: if os.environ is referenced elsewhere in the Python interpreter).

                  推荐答案

                  _environ = dict(os.environ)  # or os.environ.copy()
                  try:
                  
                      ...
                  
                  finally:
                      os.environ.clear()
                      os.environ.update(_environ)
                  

                  这篇关于Python - 临时修改当前进程的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                  Why can#39;t environmental variables set in python persist?(为什么python中设置的环境变量不能持久化?)

                        <tbody id='zgBJg'></tbody>
                        <bdo id='zgBJg'></bdo><ul id='zgBJg'></ul>
                          <i id='zgBJg'><tr id='zgBJg'><dt id='zgBJg'><q id='zgBJg'><span id='zgBJg'><b id='zgBJg'><form id='zgBJg'><ins id='zgBJg'></ins><ul id='zgBJg'></ul><sub id='zgBJg'></sub></form><legend id='zgBJg'></legend><bdo id='zgBJg'><pre id='zgBJg'><center id='zgBJg'></center></pre></bdo></b><th id='zgBJg'></th></span></q></dt></tr></i><div id='zgBJg'><tfoot id='zgBJg'></tfoot><dl id='zgBJg'><fieldset id='zgBJg'></fieldset></dl></div>
                          <legend id='zgBJg'><style id='zgBJg'><dir id='zgBJg'><q id='zgBJg'></q></dir></style></legend><tfoot id='zgBJg'></tfoot>

                          <small id='zgBJg'></small><noframes id='zgBJg'>