简体   繁体   English

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

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

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

You can use !setup.py install to do that.您可以使用!setup.py install来做到这一点。

Colab is just like a Jupyter notebook. Colab 就像一个 Jupyter 笔记本。 Therefore, we can use the !因此,我们可以使用! operator here to install any package in Colab.操作员在这里安装 Colab 中的任何包。 What !什么! actually does is, it tells the notebook cell that this line is not a Python code, its a command line script .实际上是,它告诉笔记本单元这一行不是 Python 代码,它是一个命令行脚本 So, to run any command line script in Colab, just add a !因此,要在 Colab 中运行任何命令行脚本,只需添加一个! preceding the line.的前面

For example: !pip install tensorflow .例如: !pip install tensorflow This will treat that line (here pip install tensorflow ) as a command prompt line and not some Python code.这会将该行(此处为pip install tensorflow )视为命令提示符行,而不是某些 Python 代码。 However, if you do this without adding the !但是,如果您在不添加! preceding the line, it'll throw up an error saying "invalid syntax".在该行之前,它会抛出一个错误,指出“无效的语法”。

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 :)希望这能回答你的问题:)

Let's say you want to install scipy.假设您要安装 scipy。 Here is the code to install it:这是安装它的代码:

!pip install scipy

A better, more modern, answer to this question is to use the %pip magic, like:这个问题的一个更好、更现代的答案是使用%pip魔法,例如:

%pip install scipy

That will automatically use the correct Python version.这将自动使用正确的 Python 版本。 Using !pip might be tied to a different version of Python, and then you might not find the package after installing it.使用!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 %conda magic for doing the same with conda.顺便说一句,还有一个%conda魔法可以用 conda 做同样的事情。

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.加入派对很晚,但作为补充,不久前我遇到了 Seaborn 的一些问题,因为 CoLab 安装了一个没有更新的带有 !pip 的版本。 In my specific case, I couldn't use Scatterplot, for example.例如,在我的特定情况下,我不能使用 Scatterplot。 The answer to this is below:答案如下:

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 pip apply normally, so that pretty much should work.我相信pip 的所有常见规则都适用,因此几乎应该可以使用。

To import a library that's not in Colaboratory by default, you can use !pip install or !apt-get install .要导入默认情况下不在 Colaboratory 中的库,您可以使用!pip install!apt-get install

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM