ModuleNotFoundError:没有名为 openai 的模块

Aut*_*uto 6 python interpreter visual-studio-code

import requests
from bs4 import BeautifulSoup
import openai 

#write each line of nuclear.txt to a list
with open('nuclear.txt', 'r') as f:
    lines = f.readlines()

#remove the newline character from each line
lines = [line.rstrip() for line in lines]

#gather the text from each website and add it to a new txt file
for line in lines:
    r = requests.get(line)
    soup = BeautifulSoup(r.text, 'html.parser')
    text = soup.get_text()
    with open('nuclear_text.txt', 'a') as f:
        f.write(text)

Run Code Online (Sandbox Code Playgroud)

我正在尝试导入 openai,但它不断抛出错误“未找到模块”。我已经完成了 pip install openai 并下载了它,但它似乎是错误的 python 版本。如何选择正确的 pip 安装位置?我正在使用 VSCode

pip 安装 openai

Jia*_*eDu 11

按照以下步骤安装openai当前解释器的软件包
  1. 运行以下代码

    import sys
    print(sys.executable)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 获取当前解释器路径

    在此输入图像描述

  3. 复制路径并openai在终端中使用以下命令安装

    C:\WorkSpace\pytest10\.venv\Scripts\python.exe -m pip install openai
    
    Run Code Online (Sandbox Code Playgroud)

    将上面命令中的路径修改为你得到的解释器路径。

    在此输入图像描述


小智 6

尝试使用pip3 install openai,因为它为 python3 安装 openai,而不是 python2 (如果已安装)。如果你只有 python3,pip 和 pip3 基本上是一样的(我认为)。