Running Jupyter via command line on Windows
Tags: python, jupyter, windows
Published: 2023-10-29 14:05:45

I had the exact same problem and it was driving me crazy. Other answers provide a solution, but they don't explain why you and I are having this problem.

I will try to explain why this is happening and then provide some solutions.

You can go to the end to see the TL;DR.

1)What's going on? Why is this error happening?
=================================================

I'll try to make a step-by-step answer so everything is explained clearly. 
If you think it's too basic at the beginning, go to the end of this "article".

I'll first start with common things like running the `python` shell from the terminal or running `pip`. You'll see why you can do that from the terminal and we'll end up on *why* and *how* you can run the `jupyter` notebook from the terminal as well.

Ready? Let's start!
___________

**Have you ever wondered why you can type `python` in the terminal (command prompt) and suddenly start the Python interpreter?**

```python
    Microsoft Windows [Version 10.0.18363.1440]
    (c) 2019 Microsoft Corporation. All rights reserved.
    
    C:\Users\YOUR-USERNAME>python
    Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
```

You probably already know (but maybe don't) that this is because Python was added to the Windows `PATH` environment variable. You probably did it at [installation][1] time or [afterwards][2].

**But, what is this [PATH][3] environment variable?**

> It basically allows you to run any executables, that are located inside
> the paths specified in the variable, at the command prompt without
> having to give the full path to the executable.

You can check the content of that `PATH` variable with:

```python
    >>> import sys
    >>> for path in sys.path:
            print(path)
```

 ```python
    C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\python39.zip
    C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\DLLs
    C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\lib
    C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39
    C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\lib\site-packages
    ... (some other paths were taken out for clarity)
```
    
You can see this folder: `C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39`. This is the place where Python version 3.9 is installed.

Let's check its content:

```python
    <DIR>   DLLs
    <DIR>   Doc
    <DIR>   etc
    <DIR>   include
    <DIR>   Lib
    <DIR>   libs
    <DIR>   Scripts
    <DIR>   share
    <DIR>   tcl
    <DIR>   Tools
            LICENSE.txt
            NEWS.txt
            python.exe
            python3.dll
            python39.dll
            pythonw.exe
            vcruntime140.dll
            vcruntime140_1.dll
```

*Voilà!* We have the `python.exe` file (an [executable][4]). We have a Python executable file in the `PATH`, that's why you can start the Python interpreter from the terminal with just typing `python`. If this wasn't the case you would have to type the full path to the executable file in the terminal:

```python
    C:\Users\YOUR-USERNAME> C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\python)
```

Instead of just:

```python
    C:\Users\YOUR-USERNAME> python
```
_______________

**And what about when you use `pip`?**

It's the same principle. You can run `pip` from the terminal because there is a `pip` executable file in the `PATH` variable.

If you go to `C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Scripts\` (which is in the `PATH` showed above) you'll see many executables files. One of them is `pip`. Actually I have three versions: `pip`, `pip3.9` and `pip3`.

The `Scripts` [folder][1] allows exectuable files to be run from the terminal. Like `pip` or other libraries that you intend to run directly from the terminal. The `Scripts` folder:

> ...is not intended for you, it's for scripts that are installed as
> components of modules that you install. For example, pip is a module,
> but it also has a wrapper script by the same name, pip, which will be
> installed in that directory.
> 
> If you put something there and it is properly in your PATH, then it
> should be executable

That wrapper script would be the `pip` executable file. When [this executable][5] file is run, it locates the `pip` folder in the Python installation folder and runs `pip`.

But you could also run `pip` directly from the installation folder (`C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Lib\site-packages`), without needing the executable `pip` file.

*But, how can you do it?*

I'm glad you ask. There is a Python [way][6] to run modules as the main module (without the need to import it).

```python
    python -m pip
```

When you run a module directly its name becomes `__main__`. What `-m` does is: 
> Search `sys.path` for the named module and execute its contents as the `__main__` [module][7].

What is `__main__`?
> `'__main__'` is the name of the scope in which top-level code executes.
>
> A module’s `__name__` is set equal to `'__main__'` when read from standard
> input, a script, or from an interactive prompt.
>...

I guess that the `pip` executable does something similar, or at least, has the same effect: to start `pip`.

_____________

2)What does this have to do with the Jupyter Notebook?!
========

Think of the `Jupyter Notebook` as the same as `pip`. If you want to run `jupyter` in the terminal, you need an executable that it's on the `PATH`.

We have already seen that the executables of modules like `pip` or `jupyter` are located here `C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Scripts\`.

If I check the content of the folder I see this:

```python
    easy_install-3.9.exe
    easy_install.exe
    f2py.exe
    jsonschema.exe
    jupyter-bundlerextension.exe
    jupyter-console.exe
    jupyter-nbconvert.exe
    jupyter-nbextension.exe
    jupyter-notebook.exe
    jupyter-qtconsole.exe
    jupyter-serverextension.exe
    jupyter-trust.exe
    pip.exe
    pip3.9.exe
    pip3.exe
```

I see the already mentioned `pip`, `pip3.9` and `pip3`. But I don't see `jupyter` (the word "*jupyter*" alone).

If I type `jupyter` in the terminal I get the error that started all:

    'jupyter' is not recognized as an internal or external command, operable program or batch file.

Finally we've reached an answer to your question!!!
________

**'jupyter' is not recognized as a command because there is no executable file in the `Scripts` folder called `jupyter`.**
________

So, let's try a different executable. What about `jupyter-notebook`?

*BINGO!* The notebook is running!

```python
    Serving notebooks from local directory:
    C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Scripts
    Jupyter Notebook 6.3.0 is running at:
    http://localhost:8888/?token=... (edited)
    or http://127.0.0.1:8888/?token=... (edited)
    Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
```

I don't know why I don't have a `jupyter` executable called 'jupyter'. The [official documentation][8] says to use `jupyter notebook` on the terminal, but it seems that in some cases it doesn't work. And I think it has to do with what I mentioned above: *there is no `jupyter` exectuable in the `Scripts` folder.*

___________

If you remember, I told you that you can run `pip` as the main module using `python -m pip`.

It happens that you can do the same with `jupyter`.We just need to know how to call it. As in with `pip`, we have to check the folder where 3rd party libraries are installed: `C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Lib\site-packages`.

You'll see `jupyter_console`, but this just creates an interactive notebook in the terminal, not exactly what you were looking for. You're also going to find folders ending with `.dist.info`, like `jupyter_console-6.4.0.dist-info`. This is just [metadata][9] of the Wheel Binary Package builder. You'll also see a folder like `jupyterlab_pygments`, but [that's][10] for `JupyterLab`. We want to use the classic Jupyter notebook.

What we want is to run `notebook`. How do we know this?

You'll see in the folder `site-packages` the folder (*package*) `notebook`. Inside there is a [file called][11] `__main__.py`:

```python
    #__main__.py
    if __name__ == '__main__':
        from notebook import notebookapp as app
        app.launch_new_instance()
```

It's calling `notebookapp.py` [which is][12] a *"A tornado based Jupyter notebook server."* Yes, this is what we need.

We can [see][12] that `launch_new_instance` in the `notebookapp` calls `launch_instance()`, [which][13] "*launches an instance of a Jupyter Application*".

Perfect! We are in the correct folder. To run the jupyter notebook from the Python interactive shell we have to run the notebook package with:

```python
    python -m notebook
```
____________

3)*** SUMMARY: SOLUTION ***
=================

tl;dr:

I have explained and showed why this error is happening.

Now let's summarize the solutions:

1. To know the name of the `jupyter` executable (in the `Scripts` folder), so you can run directly from the terminal (Command Prompt) as:

```python
        jupyter notebook
```

or as:

```python
        jupyter-notebook
```

Or whatever name you have.

2. Run the notebook as the main module from Python:

```python
        python -m notebook
```

I hope this helps you as much as it helped me. I'm open to your comments and suggestions.


NOTE: Originally posted in StackOverflow
https://stackoverflow.com/a/66938692/6710903

  [1]: https://docs.python.org/3/using/windows.html#finding-the-python-executable
  [2]: https://geek-university.com/python/add-python-to-the-windows-path/
  [3]: https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/
  [4]: https://en.wikipedia.org/wiki/Executable
  [5]: https://www.reddit.com/r/Python/comments/6zxadq/what_is_the_cpython27scripts_folder_for/
  [6]: https://docs.python.org/3/using/cmdline.html#cmdoption-m
  [7]: https://docs.python.org/3/library/__main__.html#module-__main__
  [8]: https://jupyter.org/install
  [9]: https://www.python.org/dev/peps/pep-0427/#file-contents
  [10]: https://mybinder.org/v2/gh/jupyterlab/jupyterlab-demo/master?urlpath=lab/tree/demo
  [11]: https://github.com/jupyter/notebook/blob/master/notebook/__main__.py
  [12]: https://github.com/jupyter/notebook/blob/master/notebook/notebookapp.py
  [13]: https://github.com/jupyter/jupyter_core/blob/e69a436529838dce8b41e6d4508f94930c87b559/jupyter_core/application.py#L251