Python OpenAI库安装问题详解与解决方案

因为要使用Moonshot平台的API开发一些小工具,用到OpenAI库(Moonshot API兼容OpenAI). 安装后导入却发现有依赖问题,解决过程如下.

问题描述

笔者使用的Conda管理Python环境,为项目创建了单独的Python环境,版本选择现在兼容性较好的3.10

conda create -n llm python=3.10
conda activate llm

创建后查看Python版本是3.10.16.
随后升级pip

python -m pip install --upgrade pip

注意,pip install --upgrade pip这样写在Win10环境下报错了.

然后根据OpenAI官方文档进行OpenAI库的安装

pip install openai

到这里出现了error:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
astroid 2.15.1 requires wrapt<2,>=1.11; python_version < "3.11", which is not installed.
pylint 2.17.1 requires dill>=0.2; python_version < "3.11", which is not installed.
pylint 2.17.1 requires tomlkit>=0.10.1, which is not installed.

很显然,有依赖没能安装. 因为之前在别的环境中出现过因为依赖没能成功安装而导致import失败,这里需要解决这个问题.

解决

从错误消息中可以看到是wrapt,dill,tomlkit这三个库没能安装,先尝试手动安装.
经过尝试,发现这三个库也是有依赖关系的,需要按照一定顺序安装,整理后的命令如下:

pip install "tomlkit>=0.10.1"
pip install "dill>=0.2"
pip install "wrapt<2,>=1.11"

安装完测试一下导入

python
Python 3.10.16 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:19:12) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import openai
>>> print(openai.__version__)
1.57.4
>>>

能够正常打印版本,问题解决

作者:MerlinTheMagic

物联沃分享整理
物联沃-IOTWORD物联网 » Python OpenAI库安装问题详解与解决方案

发表回复