Python 如何解决:ModuleNotFoundError: No module named 'google.colab'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52620788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:08:40  来源:igfitidea点击:

How to resolve: ModuleNotFoundError: No module named 'google.colab'

pythoncondagoogle-colaboratory

提问by Ravaging Care

I want to run the command:

我想运行以下命令:

from google.colab import auth

But I am getting this error:

但我收到此错误:

ModuleNotFoundError: No module named 'google.colab'

ModuleNotFoundError:没有名为“google.colab”的模块

This is required for accessing files on google drive from python. There is a package google but not module colab in it. How to resolve this error?

这是从 python 访问谷歌驱动器上的文件所必需的。有一个包 google 但里面没有模块 colab。如何解决此错误?

回答by Hazeeb M

You can simply download google-colaband use it in local.

您可以简单地下载google-colab并在本地使用它。

pip install google-colab

回答by Dhruvan Ganesh

AFAIK, you can execute the module 'google.colab' from within colab.research.google.com

AFAIK,您可以从 colab.research.google.com 中执行模块“google.colab”

Looking at the tag condain your question. I assume that you are running the code from your local machine. Please make use of PyDrive to read from google drive on your local machine.

查看conda您问题中的标签。我假设您正在本地机器上运行代码。请使用 PyDrive 从本地机器上的谷歌驱动器读取。

References:

参考:

  1. Google Colaboratory FAQ
  2. PyDrive
  1. Google Colaboratory 常见问题解答
  2. 驱动器

回答by Ashhar Shaikh

11

11

You can use !setup.pyinstall to do that.

您可以使用!setup.pyinstall 来做到这一点。

Colab is just like a Jupyter notebook. Therefore, we can use the ! operator here to install any package in Colab. What ! actually does is, it tells the notebook cell that this line is not a Python code, its a command line script. So, to run any command line script in Colab, just add a ! preceding the line.

Colab 就像一个 Jupyter 笔记本。因此,我们可以使用 ! 操作员在这里安装 Colab 中的任何包。什么 !实际上是,它告诉笔记本单元这一行不是 Python 代码,它是一个命令行脚本。因此,要在 Colab 中运行任何命令行脚本,只需添加一个 ! 行前。

For example: !pip install tensorflow. This will treat that line (here pip install tensorflow) as a command prompt line and not some Python code. However, if you do this without adding the ! preceding the line, it'll throw up an error saying "invalid syntax".

例如:!pip install tensorflow。这会将该行(此处为 pip install tensorflow)视为命令提示符行,而不是某些 Python 代码。但是,如果您在不添加 ! 在该行之前,它会抛出一个错误,说“无效的语法”。

But keep in mind that you'll have to upload the setup.py file to your drive before doing this (preferably into the same folder where your notebook is).

但请记住,在执行此操作之前,您必须将 setup.py 文件上传到您的驱动器(最好上传到您的笔记本所在的同一文件夹中)。

Hope this answers your question

希望这能回答你的问题