跟课疑惑:Python 使用 dotenv 来管理 API 们

close up photo of keyboard

之前跟课练习的时候,我已经可以成功的用以下代码来调用 OpenAI 了。

During the earlier class session, I had adeptly utilized the subsequent code to invoke OpenAI.

import openai
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key = os.getenv('OPENAI_API_KEY')

But 中间隔了一点时间,以至于忘记了当时的 API 怎么配置好的。

But there has been a gap in between, and I’ve forgotten how did I configure the API back then?

然后,看到了注释,让我恍然大明白呀。要有一个 .env file 的。

That’s when it hit me as I saw this line of comment – I need an .env file, of course!

这就是一直 export 但是没有用的原因。export 只是写到了 系统文件里。

That’s why trying ‘export’ multiple times didn’t do the trick. Export just put the api_key into the bash_profile.

确实可以写成功。但是我的代码并不会去 export 的文件里去找。

It did placed the api_key successfully, but the code won’t find it there.

所以,在 vim 进入 .env 文件后,再 i 修改好 API 值,最后再 按 【esc退出键】,然后 :wq 保存好。

So, after vim commands of edit and i to change the .env file. Finally, esc + :wq would save and exit.

然后可以继续愉快的学习了。

Now we can get back to enjoying our learning process~~


了解 小匚的个人博客 的更多信息

订阅后即可通过电子邮件收到最新文章。

发表评论

了解 小匚的个人博客 的更多信息

立即订阅以继续阅读并访问完整档案。

继续阅读

了解 小匚的个人博客 的更多信息

立即订阅以继续阅读并访问完整档案。

继续阅读