如何在 Google 的 Colab 中安装 Python 包? [英] How do I install Python packages in Google's Colab?

查看:230
本文介绍了如何在 Google 的 Colab 中安装 Python 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 install 来做到这一点.

You can use !setup.py install to do that.

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

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.

例如:!pip install tensorflow.这会将该行(此处为 pip install tensorflow)视为命令提示符行,而不是某些 Python 代码.但是,如果您这样做时没有在该行前面添加 !,它会抛出一个错误,提示语法无效".

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".

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

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).

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

这篇关于如何在 Google 的 Colab 中安装 Python 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆