没有名为googleapiclient.discovery的模块。

12
我一直在寻找实现在线Python脚本的方法,以便让我能够按照GitHub链接所示与YouTube API 进行交互。链接在这里:https://github.com/youtube/api-samples/blob/master/python/search.py
我遇到的问题在于脚本开头的导入语句:
import argparse

from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

在线文档要求使用以下命令安装googleapiclient库:

pip install --upgrade google-api-python-client

然而,安装后我仍然收到一个错误提示,指出找不到googleapiclient.discovery。我已尝试通过pip重新安装,以下是生成的命令行输出,表明一切正常:

Requirement already up-to-date: google-api-python-client in g:\python27\lib\site-packages (1.7.4)
Requirement not upgraded as not directly required: httplib2<1dev,>=0.9.2 in g:\python27\lib\site-packages (from google-api-python-client) (0.9.2)
Requirement not upgraded as not directly required: google-auth>=1.4.1 in g:\python27\lib\site-packages (from google-api-python-client) (1.5.0)
Requirement not upgraded as not directly required: google-auth-httplib2>=0.0.3 in g:\python27\lib\site-packages (from google-api-python-client) (0.0.3)
Requirement not upgraded as not directly required: six<2dev,>=1.6.1 in g:\python27\lib\site-packages (from google-api-python-client) (1.10.0)
Requirement not upgraded as not directly required: uritemplate<4dev,>=3.0.0 in g:\python27\lib\site-packages (from google-api-python-client) (3.0.0)
Requirement not upgraded as not directly required: rsa>=3.1.4 in g:\python27\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (3.4.2)
Requirement not upgraded as not directly required: cachetools>=2.0.0 in g:\python27\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (2.1.0)
Requirement not upgraded as not directly required: pyasn1-modules>=0.2.1 in g:\python27\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (0.2.2)
Requirement not upgraded as not directly required: pyasn1>=0.1.3 in g:\python27\lib\site-packages (from rsa>=3.1.4->google-auth>=1.4.1->google-api-python-client) (0.1.9)
pyasn1-modules 0.2.2 has requirement pyasn1<0.5.0,>=0.4.1, but you'll have pyasn1 0.1.9 which is incompatible.

我做错了什么?谢谢。

我正在Windows 10上使用Python 2.7,我应该补充说明... - gdogg371
是的,我也尝试过上述安装方法。 - gdogg371
googleapiclient需要您安装uritemplate.py。您尝试过了吗:pip install uritemplate.py?如果是这样,请尝试使用以下命令进行强制重新安装:pip install --force-reinstall uritemplate.py - SamrajM
我刚刚尝试了一下,也没有运气。 - gdogg371
让我们在聊天中继续这个讨论 - SamrajM
显示剩余2条评论
3个回答

13

如果您正在运行Python3 (python --version),也许您应该运行以下命令:

 pip3 install google-api-python-client

另一种解决此问题的快速方法是将软件包安装在与您的代码相同的文件夹中:

 pip install google-api-python-client -t ./

这不是最理想的,但肯定可以实现。


或者,如果你喜欢将外部库移动到一个 lib/ 文件夹中:

pip install google-api-python-client -t ./lib

在最后一种情况下,您还需要在Python代码开头添加以下内容:
import os
import sys
file_path = os.path.dirname(__file__)
module_path = os.path.join(file_path, "lib")
sys.path.append(module_path)

from googleapiclient.discovery import build

1
请注意,如果您安装了两个版本,请确保以python3而不是python运行。 - Crazenezz
请注意pip install的输出信息。Pip可能会警告您已将库安装到不在您的PATH上的目录中。 - Mark

1

我在尝试在VS Code中编写涉及“YouTube API”的代码时遇到了类似的问题。在许多在线编码论坛的建议下,我运行了

pip install --upgrade google-api-python-client

但这并没有帮助。

以下步骤解决了我的问题:

在VSCode中进入“设置”(Windows上的Ctrl +,),在“搜索设置”中输入venv,在“Python:Venv路径”标题下输入您的虚拟环境路径,如下图所示:

VS Code中Python:Venv路径的设置

然后,在VS Code中单击Python解释器,如下所示:(所选解释器反映在VS Code编辑器左下角)

Python解释器路径


0

此解决方案仅适用于使用“Visual Studio”构建Flask应用程序的用户。(其他人也可以尝试)

您需要检查的唯一事项是“我从哪里导入所有库”,在创建新环境时按照以下过程进行操作。

Python环境 >(右键单击)> 添加新环境 > 选中“在Python环境窗口中查看”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接