从.env文件中导出变量并从os.environ中获取它们,不使用python-dotenv。

huangapple go评论84阅读模式
英文:

Export variables from .env file and get them from os.environ without python-dotenv

问题

我想在我的Python脚本中使用一些环境变量。我创建了一个.env文件,其中包含以下内容:

  1. TEST='test'

然后创建了main.py

  1. import os
  2. print(os.getenv("TEST"))
  3. print(os.environ['TEST'])

在相同的目录中执行以下命令:

  1. $ source .env
  2. $ echo $TEST
  3. test

然后运行:

  1. $ python3 main.py
  2. None
  3. Traceback (most recent call last):
  4. File "/Users/me/temp/pt/main.py", line 3, in <module>
  5. print(os.environ['TEST'])
  6. File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/os.py", line 679, in __getitem__
  7. raise KeyError(key) from None
  8. KeyError: 'TEST'

为什么在这种情况下 os.getenv 不起作用,只有在执行 export TEST=test 或使用 python-dotenv 模块后才能正常工作?

英文:

I want to use couple env variables in my python script. I created .env file with:

  1. TEST=&#39;test&#39;

then create main.py:

  1. import os
  2. print(os.getenv(&quot;TEST&quot;))
  3. print(os.environ[&#39;TEST&#39;])

execute in same dir:

  1. $ source .env
  2. $ echo $TEST
  3. test

then run:

  1. $ python3 main.py
  2. None
  3. Traceback (most recent call last):
  4. File &quot;/Users/me/temp/pt/main.py&quot;, line 3, in &lt;module&gt;
  5. print(os.environ[&#39;TEST&#39;])
  6. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/os.py&quot;, line 679, in __getitem__
  7. raise KeyError(key) from None
  8. KeyError: &#39;TEST&#39;

Why in this case os.getenv is not work and work only after export TEST=test executing or with python-dotenv module using?

答案1

得分: 1

TL;DR 不要使用 source 命令处理 dotenv 文件;它们不是 shell 脚本。


问题在于 source 命令并不 导出 文件中定义的任何变量;你只是定义了普通的 shell 变量,它们并不出现在新的 Python 进程的环境中。

你可以通过启用 -a 选项,自动导出文件中定义的任何变量来解决这个问题。

  1. $ set -a
  2. $ source .env
  3. $ python3 main.py
  4. test
  5. test

然而,这里的更大问题是 .env 文件 不是 shell 脚本。dotenv 遵循的引号规则是不同的。比较一下:

  1. $ cat .env
  2. TEST=test echo hello
  3. $ cat main.py
  4. import os
  5. import dotenv
  6. x = dotenv.dotenv_values('.env')
  7. print(os.getenv("TEST"))
  8. print(x['TEST'])
  9. $ set -a
  10. $ source .env
  11. hello
  12. $ python3 main.py
  13. None
  14. test echo hello

使用 source 命令不会定义一个名为 TEST 的变量;相反,它执行 echo 命令,该命令忽略仅针对 它自己 环境设置的 TEST 值。

使用 dotenv 处理文件会生成一个名为 TEST 的变量,其值设置为完整的字符串 test echo hello

英文:

TL;DR Don't use source to process dotenv files; they are not shell scripts.


The immediate problem is that source does not export any of the variables defined by the file; you are only defining regular shell variables that do not appear in the environment of the new Python process.

You can work around that by enabling the -a option to auto-export any variables defined in the file.

  1. $ set -a
  2. $ source .env
  3. $ python3 main.py
  4. test
  5. test

However, the bigger problem here is that .env files are not shell scripts. The quoting rules followed by dotenv are different. Compare:

  1. $ cat .env
  2. TEST=test echo hello
  3. $ cat main.py
  4. import os
  5. import dotenv
  6. x = dotenv.dotenv_values(&#39;.env&#39;)
  7. print(os.getenv(&quot;TEST&quot;))
  8. print(x[&#39;TEST&#39;])
  9. $ set -a
  10. $ source .env
  11. hello
  12. $ python3 main.py
  13. None
  14. test echo hello

Sourcing the file does not define a variable TEST; instead, it executes the echo command that ignores the value of TEST set only for its environment.

Processing the file with dotenv produces a variable named TEST set to the full string test echo hello.

答案2

得分: 0

尝试以下步骤:

  1. 安装 python-dotenv:
    • pip install python-dotenv
  2. 在你的 Python 脚本中导入该包:
    • from dotenv import load_dotenv
    • import os
  3. 从 .env 文件加载环境变量:
    • load_dotenv()
  4. 获取名为 'TEST' 的环境变量:
    • my_var = os.getenv('TEST')
英文:

Try these steps:

  1. pip install python-dotenv
  2. Import the package in your python script:
    • from dotenv import load_dotenv
    • import os
  3. Load the environment variables from the .env file:
    • load_dotenv()
  4. my_var = os.getenv('TEST')

huangapple
  • 本文由 发表于 2023年2月23日 21:45:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定