每次运行整个笔记本时,如何防止运行“ pip install ...”? [英] How to prevent 'pip install ...' running every time I run the whole notebook?

查看:133
本文介绍了每次运行整个笔记本时,如何防止运行“ pip install ...”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的大多数python笔记本往往都需要使用

Most of the python notebooks I run tend to need some setup for the initial run, using

! pip install ...

每次笔记本运行时执行设置代码效率低下,所以我会宁愿避免这种情况。另外,我也不想将设置代码移到其他笔记本上,因为通常只需几行代码。

Executing the setup code every time the notebook is run is inefficient, so I would prefer to avoid that. Also, I don't want to move the setup code to a different notebook because usually it is just a few lines of code.

推荐答案

<对我来说,解决方案是运行一个小的单行python脚本,该脚本仅尝试导入模块。如果导入成功,则不会运行pip install命令。相反,如果导入不成功,则运行pip install命令。

The solution for me was to run a small one line python script that only tries to import the module. If the import was successful, the pip install command does not get run. Conversely, if the import was unsuccessful the pip install command is run.

! python -c 'import cloudant' || pip install cloudant --user

双管道是bash语句,可以认为等同于'

The double pipe is a bash statement that can be considered equivalent to an 'or' statement in a programming language.

上面的示例安装了cloudant python库。只需更改您要安装的库的上述行即可。

The above example installs the cloudant python library. Just change the above line for the libraries you wish to install.

这篇关于每次运行整个笔记本时,如何防止运行“ pip install ...”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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