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

890 views Asked by At

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

There are 1 answers

0
Chris Snow On BEST ANSWER

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.