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

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

执行这个命令:

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,出来很多数据,我随意的点击了一个进去,看到了里面的内容之后,我如遭雷击,搞半天不是我安装的方式不对,而是我安装的包名错误,因为导入的代码如下

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

pip install dotenv

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

所以正确的安装命令是

pip install python-dotenv

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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 05:14:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 05:14:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 05:14:04       58 阅读
  4. Python语言-面向对象

    2024-07-15 05:14:04       69 阅读

热门阅读

  1. C# Winform之propertyGrid控件使用详解和分组设置

    2024-07-15 05:14:04       26 阅读
  2. ComfyUl提示词技巧

    2024-07-15 05:14:04       29 阅读
  3. 基于智能算法的品牌视觉识别系统优化研究

    2024-07-15 05:14:04       23 阅读
  4. Windows图形界面(GUI)-DLG-C/C++ - 图形界面程序结构

    2024-07-15 05:14:04       29 阅读
  5. 自学黑客

    2024-07-15 05:14:04       21 阅读
  6. 归并排序(递归实现)

    2024-07-15 05:14:04       25 阅读
  7. SpinalHDL之VHDL 和 Verilog 生成

    2024-07-15 05:14:04       25 阅读
  8. 等保测评助力网络安全治理现代化

    2024-07-15 05:14:04       20 阅读
  9. 去中心化预言机是什么

    2024-07-15 05:14:04       32 阅读