没有名为googleapiclient.discovery的模块

时间:2018-07-28 12:27:44

标签: python pip youtube-api kodi

我一直在寻求实现我在网上找到的示例Python脚本,以允许我根据找到的GitHub链接here

与YouTube API进行交互。

我遇到的问题是开头的导入语句:

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.

我在做什么错了?

谢谢

3 个答案:

答案 0 :(得分:2)

如果您正在运行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 :(得分:0)

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

您唯一需要检查的是“我从哪里导入我的所有库”在创建新环境时遵循以下流程。

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

答案 2 :(得分:0)

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

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

但它没有帮助。

采取以下步骤为我解决了问题:

在 VSCode 中,转到“设置”(Windows 上为 Ctrl + ,),在“搜索设置”中输入 venv,然后在“Python: Venv Path”标题下输入虚拟环境的路径为在以下屏幕截图中看到:

settings for Python: Venv Path in VS Code

然后,在VS Code中点击Python解释器,如下图:(选择的解释器反映在VS Code编辑器的左下角)

Python interpreter path