如何安装dotenv,避坑指南,安装包的包名有误?

嗨,大家好,我是蓝若姐姐。最近在研究AI大模型,想写一个调用openai接口的demo,结果发现在装一个三方库的时候一直报错,mac电脑安装dotenv报错,具体情况是

执行这个命令:

python 复制代码
pip install dotenv

遇到报错:error: subprocess-exited-with-error,pip subprocess to install backend dependencies did not run successfully

以下是完整报错

复制代码
  × pip subprocess to install backend dependencies did not run successfully.
  │ exit code: 1
  ╰─> [41 lines of output]
      Collecting distribute
        Using cached distribute-0.7.3.zip (145 kB)
        Preparing metadata (setup.py): started
        Preparing metadata (setup.py): finished with status 'error'
        error: subprocess-exited-with-error
      
        × python setup.py egg_info did not run successfully.
        │ exit code: 1
        ╰─> [22 lines of output]
            Traceback (most recent call last):
              File "<string>", line 2, in <module>
              File "<pip-setuptools-caller>", line 14, in <module>
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/setuptools/__init__.py", line 2, in <module>
                from setuptools.extension import Extension, Library
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/setuptools/extension.py", line 5, in <module>
                from setuptools.dist import _get_unpatched
              File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
              File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
              File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
              File "/Users/admin/Desktop/script/learn_ai/venv/lib/python3.11/site-packages/_virtualenv.py", line 89, in exec_module
                old(module)
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/setuptools/dist.py", line 7, in <module>
                from setuptools.command.install import install
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/setuptools/command/__init__.py", line 8, in <module>
                from setuptools.command import install_scripts
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/setuptools/command/install_scripts.py", line 3, in <module>
                from pkg_resources import Distribution, PathMetadata, ensure_directory
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/pkg_resources.py", line 1518, in <module>
                register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
            [end of output]
      
        note: This error originates from a subprocess, and is likely not a problem with pip.
      error: metadata-generation-failed
      
      × Encountered error while generating package metadata.
      ╰─> See above for output.
      
      note: This is an issue with the package mentioned above, not pip.
      hint: See above for details.
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

我尝试了很多的方法,大概包括以下几种:

  1. 更新 pip 到最新版本:

    python -m pip install --upgrade pip

2.尝试重新创建虚拟环境:

复制代码
# 删除原有虚拟环境
rm -rf venv

# 创建新的虚拟环境
python -m venv venv
source venv/bin/activate

3.清理缓存并重新安装 dotenv 包:

复制代码
pip cache purge
pip install dotenv

4.尝试手动下载 dotenv 包并安装:

复制代码
# 下载 dotenv 包
wget <https://files.pythonhosted.org/packages/b6/ad/b1d7e6933c8ddd910efed44c8ffea8bf76e1c10b0114e06e0e78055ad10/dotenv-0.0.5.tar.gz>

# 解压并安装
tar -xzf dotenv-0.0.5.tar.gz
cd dotenv-0.0.5
python setup.py install

以上方法全部尝试过,还是不行,当然这上面大部分方案都是询问的chatGPT,最终没能解决问题,但是我并没有放弃,我可不是那么容易放弃的人,chatGPT不行,我转战谷歌,之前在谷歌搜索:如何安装dotenv,出来很多数据,我随意的点击了一个进去,看到了里面的内容之后,我如遭雷击,搞半天不是我安装的方式不对,而是我安装的包名错误,因为导入的代码如下

所以我想当然的使用了这个命令:

python 复制代码
pip install dotenv

结果dotenv根本不是正确的包名,正确的包名是:python-dotenv

所以正确的安装命令是

python 复制代码
pip install python-dotenv

后来我马不停蹄的输入这个命令执行,果然没有预期中的报错,果然成功安装,然后运行代码成功

相关推荐
测试-鹏哥15 小时前
要将ITP集成到Jenkins Pipeline中,实现开发发版时自动触发自动化测试
运维·python·测试工具·ci/cd·jenkins
程序员三藏15 小时前
Postman接口测试详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
L.EscaRC15 小时前
Lua语言知识与应用解析
java·python·lua
清空mega15 小时前
从零开始搭建 flask 博客实验(5)
后端·python·flask
起予者汝也16 小时前
Python基础入门
开发语言·python
snakecy16 小时前
cuda10 cudnn7.5--旧版本
python·学习
Owen__z16 小时前
GEE统计特定区域特定时间上的Landsat/Sentinel的影像信息
python·sentinel·gee·geemap·landsat
芯联智造16 小时前
【stm32协议外设篇】- SU03T 智能语音模块
c语言·开发语言·stm32·单片机·嵌入式硬件
川石课堂软件测试16 小时前
Python | 高阶函数基本应用及Decorator装饰器
android·开发语言·数据库·python·功能测试·mysql·单元测试
lqqjuly16 小时前
Matlab2025a实现双目相机标定~业余版
开发语言·matlab·相机标定·双目相机