Pip install from a notebook

Links: notebook, html, PDF, python, slides, GitHub

How to install a module from a notebook.

from jyquickhelper import add_notebook_menu
add_notebook_menu()

Update or install a module from the notebook

Running pip install from the command line requires to be in the right folder. And sometimes, several python installations interfere between each others. Why doing it from the notebook itself:

try:
    # pip >= 19.3
    from pip._internal.main import main as pip_main
except Exception:
    try:
        # pip >= 10.0
        from pip._internal import main as pip_main
    except Exception:
        # pip < 10.0
        from pip import main as pip_main
pip_main("install -q qgrid".split())
0

Interesting options

Avoid installing dependencies

try:
    pip_main("install -q qgrid --no-deps".split())
except Exception as e:
    print(e)

Upgrade

try:
    pip_main("install -q qgrid --upgrade --no-deps".split())
except Exception as e:
    print(e)

No cache

By default, pip uses cached version. So, if a module has just been updated, pip might choose to use the previous version. To tell it not to do so:

pip_main("install -q qgrid --upgrade --no-deps --no-cache-dir".split())
0

For the hackathon…

pip_main("install pyquickhelper pyensae ensae_projects --upgrade --no-deps --no-cache-dir".split())