一个好玩的 Python 依赖坑

February 8, 2025

我在本机跑 PuppyAgent 的 API 服务,代码里有一句 from dotenv import load_dotenv,用来读项目的 .env 配置文件。缺什么装什么嘛,我敲了 pip install dotenv。

翻回当时的终端记录,我一共猜了四次。第一次猜 dotenv,第二次猜 dotenv-binary,第三次又猜回了 dotenv。第四次才写对:python-dotenv。最好笑的是,项目的 requirements.txt 里本来就写着答案。

那天下午我点开了 GitHub 上的一条 issue。发帖人也敲了 pip install dotenv。偏偏 PyPI 上真有项目叫这个名字,pip 找到了 dotenv-0.0.5.tar.gz,一路走到构建依赖报错。底下已经有人开始怀疑打包工具 setuptools 了。维护者 theskumar 的回复倒很短。1

@VrushankiPatel The install command for this library is

pip install python-dotenv

and not

pip install dotenv

Cheers!

原来这两个名字各管一步。pip 按发布名 python-dotenv 去找安装包,装好以后,Python 就能按代码里的名字 dotenv 找到它。光看那行 import,还真猜不出前面要加个 python-。2

后来再看,“Cheers!” 底下已经攒了 👍 58、😄 2、🎉 5、❤️ 1。要是 GitHub 再给一个“我也装过 dotenv-binary”的按钮,我知道谁能贡献第一票。

Footnotes

  1. VrushankiPatel. (2023, July 7). Installing backend dependencies error [Issue #472]. GitHub. 原帖与安装日志. Kumar, S. [theskumar]. (2023, July 10). 维护者回复全文及表情反应. GitHub. 表情数按 2026 年 11 月 20 日查阅时所见页面记录。 ↩

  2. Python Packaging Authority. (n.d.). Distribution package vs. import package. Python Packaging User Guide. 发布名与导入名. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-02-08). 一个好玩的 Python 依赖坑. https://caiborui.com/blog/2025-02-08-zh-pip-install-python-dotenv

@misc{borui2025-bc872b20,
  author = {borui},
  title = {{一个好玩的 Python 依赖坑}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-02-08-zh-pip-install-python-dotenv}
}