what does {sys.executable} do in jupyter notebook?

Viewed 1304

I bought a book which comes with jupyter notebook. In the first chapter, it asks me to install required libraries. It use {sys.executable} -m. I never see it before. what does {sys.executable} and -m do? also why use --user at the end?

typically, I just use ! pip install numpy==1.19.2

Anyone can help me understand it? Thank you!


import sys

!{sys.executable} -m pip install numpy==1.19.2 --user
!{sys.executable} -m pip install scipy==1.6.2 --user
!{sys.executable} -m pip install tensorflow==2.4.0 --user
!{sys.executable} -m pip install tensorflow-probability==0.11.0 --user
!{sys.executable} -m pip install scikit-learn==0.24.1 --user
!{sys.executable} -m pip install statsmodels==0.12.2  --user
!{sys.executable} -m pip install ta --user

2 Answers

Let's split this question up into multiple parts.

Part 1

From the Python documentation:

sys.executable

A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

Formatting that in, we get:

...\python.exe -m pip install <package> --user

Part 2

Also from the docs:

-m <module-name>

Search sys.path for the named module and execute its contents as the __main__ module.

This is generally the same as just pip install <package> --user, however if you have multiple versions of Python installed, the wrong version of pip might get invoked. By using -m, a matching version of pip will always be invoked.

Part 3

This time from the pip documentation:

Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on Windows. (See the Python documentation for site.USER_BASE for full details.)

Basically, this means that instead of installing to the normal package directory (which could require administrator privileges), it installs to %APPDATA%\Python, which should always be accessible as it is in your user folder.

sys.executable is refering to the Python interpreter for the current system. It comes handy when using virtual environments and have several interpreters on the same machine.

The -m option loads and execute a module as a script, here pip.

The --user is an option for pip install, see this answer describing its use.

Then the !{} is jupyter-specific syntax to execute commands in a cell if I remember correctly.

Related