如何在 Google 的 Colab 中安装 Python 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51342408/
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 19:49:47  来源:igfitidea点击:

How do I install Python packages in Google's Colab?

pythonpipjupyter-notebookgoogle-colaboratorysetup.py

提问by Lin Jianjie

In a project, I have e.g. two different packages, How can I use the setup.py to install these two packages in the Google's Colab, so that I can import the packages?

在一个项目中,我有两个不同的包,如何使用 setup.py 在 Google 的 Colab 中安装这两个包,以便我可以导入包?

回答by Ashutosh Pathak

You can use !setup.py installto do that.

你可以用它!setup.py install来做到这一点。

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 !precedingthe 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.pyfile to your drivebefore doing this (preferably into the same folder where your notebook is).

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

Hope this answers your question :)

希望这能回答你的问题:)

回答by Ravi G

lets say you want to install scipy,

假设你想安装 scipy,

Here is the code to install it

这是安装它的代码

!pip install scipy

回答by DuckRodgers

Joining the party late, but just as a complement, I ran into some problems with Seaborn not so long ago, because CoLab installed a version with !pip that wasn't updated. In my specific case, I couldn't use Scatterplot, for example. The answer to this is below:

加入晚会,但作为补充,不久前我遇到了 Seaborn 的一些问题,因为 CoLab 安装了一个没有更新的带有 !pip 的版本。例如,在我的特定情况下,我不能使用 Scatterplot。答案如下:

To install the module, all you need is:

要安装该模块,您只需要:

!pip install seaborn

To upgrade it to the most updated version:

要将其升级到最新版本:

!pip install --upgrade seaborn

If you want to install a specific version

如果要安装特定版本

!pip install seaborn==0.9.0

I believe all the rules common to pipapply normally, so that pretty much should work.

我相信pip 的所有常见规则都适用,因此几乎应该可以使用。

回答by Doug Blank

A better, more modern, answer to this question is to use the %pipmagic, like:

对这个问题的一个更好、更现代的答案是使用%pip魔法,例如:

%pip install scipy

That will automatically use the correct Python version. Using !pipmight be tied to a different version of Python, and then you might not find the package after installing it.

这将自动使用正确的 Python 版本。使用!pip可能与不同版本的 Python 相关联,然后您可能在安装后找不到该软件包。

And in colab, the magic gives a nice message and button if it detects that you need to restart the runtime if pip updated a packaging you have already imported.

在 colab 中,如果 pip 更新了您已经导入的包,它会在检测到您需要重新启动运行时时提供一个很好的消息和按钮。

BTW, there is also a %condamagic for doing the same with conda.

顺便说一句,%conda用 conda 做同样的事情也有一种魔力。

回答by keramat

  1. Upload setup.py to drive.
  2. Mount the drive.
  3. Get the path of setup.py.
  4. !python PATH install.
  1. 上传 setup.py 到驱动器。
  2. 安装驱动器。
  3. 获取setup.py的路径。
  4. !python 路径安装。