5

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

roudan
  • 3,082
  • 5
  • 31
  • 72
  • 6
    `sys.executable` is just the path to the running python executable. – Pedro Maia Dec 14 '21 at 03:17
  • 5
    Running `pip` directly can cause problems if the executable that's found doesn't correspond to the Python version you're using - see the infinite number of "I installed X, but `import X` doesn't work" questions here on Stack Overflow. This approach guarantees that the right `pip` version gets run. – jasonharper Dec 14 '21 at 03:25
  • Not, `!` means it's an Jupyter/Ipython. Specific thing, and {} allows interpolation – juanpa.arrivillaga Dec 14 '21 at 03:31
  • I'm a bit confused by the background here. If you're already familiar with Python and Numpy, what's the book for? – Karl Knechtel Dec 14 '21 at 03:42
  • @KarlKnechtel For Jupyter... – Makonede Dec 14 '21 at 15:29
  • Well, if the book is supposed to teach you about Jupyter, and the very first thing it tells you to do with Jupyter is something that you can't understand by reading the surrounding parts of the book... maybe it isn't a very good book (at least for you)? – Karl Knechtel Dec 15 '21 at 02:08

2 Answers2

7

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.

Makonede
  • 442
  • 1
  • 6
  • 13
2

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.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75