How to prevent 'pip install …' running every time I run the whole notebook?

拜拜、爱过 提交于 2019-12-24 00:38:06

问题


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.


回答1:


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

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

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



来源:https://stackoverflow.com/questions/41481291/how-to-prevent-pip-install-running-every-time-i-run-the-whole-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!