如何安装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 "", line 2, in 
              File "", line 14, in 
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/setuptools/__init__.py", line 2, in 
                from setuptools.extension import Extension, Library
              File "/private/var/folders/9t/szjqc1s14q3gvjbtss8bb34c0000gn/T/pip-install-1q18f29_/distribute_04ff76157b094138a649e02182c658a4/setuptools/extension.py", line 5, in 
                from setuptools.dist import _get_unpatched
              File "", line 1176, in _find_and_load
              File "", line 1147, in _find_and_load_unlocked
              File "", 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 
                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 
                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 
                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 
                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 

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

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

如何安装dotenv,避坑指南,安装包的包名有误?_第1张图片

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

pip install dotenv

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

所以正确的安装命令是

pip install python-dotenv

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

如何安装dotenv,避坑指南,安装包的包名有误?_第2张图片

你可能感兴趣的:(AI大模型,python,开发语言,chatgpt)