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

    1. <small id='371Du'></small><noframes id='371Du'>

      • <bdo id='371Du'></bdo><ul id='371Du'></ul>
      <tfoot id='371Du'></tfoot>
    2. 为什么 Python 看不到环境变量?

      时间:2023-07-04

      • <bdo id='ymiYE'></bdo><ul id='ymiYE'></ul>
          <tbody id='ymiYE'></tbody>

          <tfoot id='ymiYE'></tfoot>

            <legend id='ymiYE'><style id='ymiYE'><dir id='ymiYE'><q id='ymiYE'></q></dir></style></legend>

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

                <i id='ymiYE'><tr id='ymiYE'><dt id='ymiYE'><q id='ymiYE'><span id='ymiYE'><b id='ymiYE'><form id='ymiYE'><ins id='ymiYE'></ins><ul id='ymiYE'></ul><sub id='ymiYE'></sub></form><legend id='ymiYE'></legend><bdo id='ymiYE'><pre id='ymiYE'><center id='ymiYE'></center></pre></bdo></b><th id='ymiYE'></th></span></q></dt></tr></i><div id='ymiYE'><tfoot id='ymiYE'></tfoot><dl id='ymiYE'><fieldset id='ymiYE'></fieldset></dl></div>
                本文介绍了为什么 Python 看不到环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 Python 2 开发 Debian Jessie.为什么 Python 的 environ 看不到 bash 中可见的环境变量?

                I'm working on Debian Jessie with Python 2. Why can't Python's environ see environment variables that are visible in bash?

                # echo $SECRET_KEY
                xxx-xxx-xxxx
                # python
                >>> from os import environ
                >>> environ["SECRET_KEY"]
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                  File "/root/.virtualenvs/prescribing/lib/python2.7/UserDict.py", line 23, in __getitem__
                    raise KeyError(key)
                KeyError: 'SECRET_KEY'
                

                我使用 /etc/environment 设置这些环境变量 - 不确定这是否相关:

                I set these environment variables using /etc/environment - not sure if that's relevant:

                SECRET_KEY=xxx-xxx-xxx
                

                我必须运行 source/etc/environment 才能让 bash 看到它们,我认为这很奇怪.

                I had to run source /etc/environment to get bash to see them, which I thought was strange.

                更新: printenv SECRET_KEY 什么也没产生,所以我猜 SECRET_KEY 是一个shell而不是一个环境变量.

                UPDATE: printenv SECRET_KEY produces nothing, so I guess SECRET_KEY is a shell not an environment variable.

                推荐答案

                你需要导出环境变量让子进程看到:

                You need to export environment variables for child processes to see them:

                export SECRET_KEY
                

                演示:

                $ SECRET_KEY='foobar'
                $ bin/python -c "import os; print os.environ.get('SECRET_KEY', 'Nonesuch')"
                Nonesuch
                $ export SECRET_KEY
                $ bin/python -c "import os; print os.environ.get('SECRET_KEY', 'Nonesuch')"
                foobar
                

                您可以一步结合设置和导出:

                You can combine the setting and exporting in one step:

                export SECRET_KEY=xxx-xxx-xxxx
                

                请注意,/etc/environment 中的新变量不会自动显示在您现有的 shell 中,除非您有 新登录.对于 GUI 桌面,您必须注销并再次登录,对于 SSH 会话,您必须创建新的 SSH 登录.只有这样,您才能获得一个新的进程树,其中包含更改.使用 source/etc/environment 仅设置本地"变量(文件不是脚本).请参阅 如何在不重启的情况下重新加载/etc/environment?用户.

                Note that new variables in /etc/environment do not show up in your existing shells automatically, not until you have a new login. For a GUI desktop, you'll have to log out and log in again, for SSH sessions you'll have to create a new SSH login. Only then will you get a new tree of processes with the changes present. Using source /etc/environment only sets 'local' variables (the file is not a script). See How to reload /etc/environment without rebooting? over on Super User.

                这篇关于为什么 Python 看不到环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何读取 Windows 环境变量值? 下一篇:通过 Selenium 和 python 切换到 iframe

                相关文章

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

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