如何安装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

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

相关推荐
自由与自然8 分钟前
栅格布局常用用法
开发语言·前端·javascript
_codemonster11 分钟前
python易混淆知识点(十六)lambda表达式
开发语言·python
小梁努力敲代码12 分钟前
Java多线程--单例模式
java·开发语言
Percep_gan13 分钟前
解决-bash: /usr/bin/yum: No such file or directory
开发语言·bash
缺点内向21 分钟前
如何在 C# 中将 Word 文档转换为 EMF(增强型图元文件)
开发语言·c#·word·.net
老华带你飞24 分钟前
学生宿舍管理|基于java + vue学生宿舍管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
Filotimo_31 分钟前
在java后端开发中,redis的用处
java·开发语言·redis
superman超哥1 小时前
仓颉Option类型的空安全处理深度解析
c语言·开发语言·c++·python·仓颉
2401_841495641 小时前
【LeetCode刷题】跳跃游戏Ⅱ
数据结构·python·算法·leetcode·数组·贪心策略·跳跃游戏
MyBFuture1 小时前
C# 哈希表与堆栈队列实战指南
开发语言·windows·c#·visual studio