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

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

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

问题

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

TEST='test'

然后创建了main.py

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

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

$ source .env
$ echo $TEST
test

然后运行:

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

TEST=&#39;test&#39;

then create main.py:

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

execute in same dir:

$ source .env
$ echo $TEST
test

then run:

$ python3 main.py
None
Traceback (most recent call last):
  File &quot;/Users/me/temp/pt/main.py&quot;, line 3, in &lt;module&gt;
    print(os.environ[&#39;TEST&#39;])
  File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/os.py&quot;, line 679, in __getitem__
    raise KeyError(key) from None
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 选项,自动导出文件中定义的任何变量来解决这个问题。

$ set -a
$ source .env
$ python3 main.py
test
test

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

$ cat .env
TEST=test echo hello
$ cat main.py
import os
import dotenv


x = dotenv.dotenv_values('.env')

print(os.getenv("TEST"))
print(x['TEST'])
$ set -a
$ source .env
hello
$ python3 main.py
None
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.

$ set -a
$ source .env
$ python3 main.py
test
test

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

$ cat .env
TEST=test echo hello
$ cat main.py
import os
import dotenv


x = dotenv.dotenv_values(&#39;.env&#39;)

print(os.getenv(&quot;TEST&quot;))
print(x[&#39;TEST&#39;])
$ set -a
$ source .env
hello
$ python3 main.py
None
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:

确定