问题
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