jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.76k stars 4.99k forks source link

running jupyter notebook under PyInstaller #3246

Closed leroygr closed 10 months ago

leroygr commented 6 years ago

Hi all,

I'm struggling hard to get a jupyter notebook server running under PyInstaller. I can load the webpage but it fails when trying to load the kernel:

$ ./app 
[I 21:42:58.938 NotebookApp] [jupyter_nbextensions_configurator] enabled 0.4.0
[I 21:42:58.940 NotebookApp] Serving notebooks from local directory: /home/gle/sources/3e-consultancy-tools/lte-4-wind/mockups/jupyter_notebooks/dist/app/notebook
[I 21:42:58.940 NotebookApp] 0 active kernels
[I 21:42:58.940 NotebookApp] The Jupyter Notebook is running at:
[I 21:42:58.940 NotebookApp] http://localhost:8888/?token=6279216dc32a4a692db991fbc6ff219239eb450851799f3e
[I 21:42:58.940 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 21:42:58.941 NotebookApp] 

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=6279216dc32a4a692db991fbc6ff219239eb450851799f3e
[I 21:42:59.246 NotebookApp] Accepting one-time-token-authenticated connection from 127.0.0.1
[15986:16024:0122/214259.309674:ERROR:browser_gpu_channel_host_factory.cc(107)] Failed to launch GPU process.
Created new window in existing browser session.
[I 21:43:05.129 NotebookApp] Creating new notebook in 
[I 21:43:05.738 NotebookApp] Kernel started: ae24b884-0a90-4a9e-a592-05e2a39214a5
[W 21:43:05.758 NotebookApp] 404 GET /nbextensions/jupyter-js-widgets/extension.js (127.0.0.1) 9.84ms referer=http://localhost:8888/notebooks/Untitled.ipynb?kernel_name=python2
[W 21:43:05.866 NotebookApp] 404 GET /nbextensions/widgets/notebook/js/extension.js (127.0.0.1) 2.48ms referer=http://localhost:8888/notebooks/Untitled.ipynb?kernel_name=python2
[I 21:43:08.736 NotebookApp] KernelRestarter: restarting kernel (1/5), new random ports
[I 21:43:11.752 NotebookApp] KernelRestarter: restarting kernel (2/5), new random ports
[I 21:43:14.764 NotebookApp] KernelRestarter: restarting kernel (3/5), new random ports
[W 21:43:15.796 NotebookApp] Timeout waiting for kernel_info reply from ae24b884-0a90-4a9e-a592-05e2a39214a5
[I 21:43:17.778 NotebookApp] KernelRestarter: restarting kernel (4/5), new random ports
WARNING:root:kernel ae24b884-0a90-4a9e-a592-05e2a39214a5 restarted
[W 21:43:20.798 NotebookApp] KernelRestarter: restart failed
[W 21:43:20.799 NotebookApp] Kernel ae24b884-0a90-4a9e-a592-05e2a39214a5 died, removing from map.
ERROR:root:kernel ae24b884-0a90-4a9e-a592-05e2a39214a5 restarted failed!
[W 21:43:20.834 NotebookApp] 410 DELETE /api/sessions/0dc43e0d-8dad-4ac5-bd4d-3d210b6b3d29 (127.0.0.1): Kernel deleted before session
[W 21:43:20.835 NotebookApp] Kernel deleted before session
[W 21:43:20.836 NotebookApp] 410 DELETE /api/sessions/0dc43e0d-8dad-4ac5-bd4d-3d210b6b3d29 (127.0.0.1) 4.82ms referer=http://localhost:8888/notebooks/Untitled.ipynb?kernel_name=python2

Here is my script app.py:

import matplotlib
import os
import sys

if len(sys.argv) == 1:
    sys.argv.append('notebook')
if sys.argv[1] == 'notebook':
    from notebook.notebookapp import main
    main()

if sys.argv[1] == 'kernel':
    from jupyter_client.kernelapp import main
    main()

if sys.argv[1] == 'nbconvert':
    from nbconvert.nbconvertapp import main
    main()

and the app.spec

# -*- mode: python -*-
import sys
import os
from glob import glob

block_cipher = None

a = Analysis(['app.py'],
             pathex=[os.path.realpath(os.path.curdir)],
             binaries=[],
             #datas=[("NOTES.md",'docs/NOTES.md')],
             datas=[],
             hiddenimports=['jupyter_core',
                            'jupyter_client',
                            'jupyter_console',
                            'jupyter_contrib_core',
                            'jupyter_core',
                            'jupyter_nbextensions_configurator',
                            'nbconvert',
                            'ipykernel',
                            'ipykernel_launcher',
                            'IPython',
                            'jupyter_client.kernelapp',
                            'ipywidgets',
                            'widgetsnbextension',
                            'jsonschema',
                            'zmq',
                            'notebook',
                            'notebook.notebookapp',
                            'notebook.tree.handlers',
                            'notebook.files.handlers',
                            'notebook.view.handlers',
                            'notebook.notebook.handlers',
                            'notebook.nbconvert.handlers',
                            'notebook.bundler.handlers',
                            'notebook.kernelspecs.handlers',
                            'notebook.edit.handlers',
                            'notebook.services.api.handlers',
                            'notebook.services.config.handlers',
                            'notebook.services.kernels.handlers',
                            'notebook.services.contents.handlers',
                            'notebook.services.sessions.handlers',
                            'notebook.services.nbconvert.handlers',
                            'notebook.services.kernelspecs.handlers',
                            'notebook.services.security.handlers',
                            'notebook.services.shutdown',
                            'jupyter_nbextensions_configurator'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

# add notebook extensions
import notebook as nb

nb_path = os.path.dirname(nb.__file__)

# add notebook html templates
a.datas += Tree(os.path.join(nb_path, 'templates'),
               prefix=os.path.join('notebook','templates'))

# add static files
a.datas += Tree(os.path.join(nb_path, 'static'),
               prefix=os.path.join('notebook','static'))

# add nbformat
a.datas += Tree(os.path.realpath(os.path.join(nb_path, os.path.pardir, 'nbformat')),
               prefix=os.path.join('nbformat'))

# add nbextensions
from jupyter_core.paths import jupyter_path

for d in jupyter_path():
    if os.path.exists(d):
        if os.path.exists(os.path.join(d, 'nbextensions')):
            for edi in os.listdir(os.path.join(d, 'nbextensions')):
                full_path = os.path.join(d,'nbextensions',edi)
                print(full_path)
                if os.path.exists(full_path):
                    if os.path.isdir(full_path):
                        a.datas += Tree(full_path,
                                    prefix=os.path.join('nbextensions', os.path.split(full_path)[-1]))
                    else:
                    # output path, input path, data type
                        a.datas += [(os.path.join('nbextensions',os.path.split(full_path)[-1]), full_path,'DATA')]

# add jupyter_nbextensions_configurator
a.datas += Tree(os.path.realpath(os.path.join(nb_path, os.path.pardir, 'jupyter_nbextensions_configurator')),
               prefix=os.path.join('jupyter_nbextensions_configurator'))

# add widgetsnbextension
a.datas += Tree(os.path.realpath(os.path.join(nb_path, os.path.pardir, 'widgetsnbextension', 'static')),
               prefix=os.path.join( 'widgetsnbextension', 'static'))

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='app',
          debug=False,
          strip=False,
          upx=True,
          console=True )

coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='app')

I'm working in a virtualenv on Ubuntu 16.04. Here is my python environment:

$ pip freeze
altgraph==0.15
backports-abc==0.5
backports.functools-lru-cache==1.4
backports.shutil-get-terminal-size==1.0.0
bleach==2.1.2
certifi==2018.1.18
configparser==3.5.0
cycler==0.10.0
decorator==4.2.1
dis3==0.1.2
entrypoints==0.2.3
enum34==1.1.6
functools32==3.2.3.post2
future==0.16.0
html5lib==1.0.1
ipykernel==4.8.0
ipython==5.5.0
ipython-genutils==0.2.0
ipywidgets==7.1.0
Jinja2==2.10
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.2.1
jupyter-console==5.2.0
jupyter-contrib-core==0.3.3
jupyter-core==4.4.0
jupyter-nbextensions-configurator==0.4.0
macholib==1.9
MarkupSafe==1.0
matplotlib==2.1.2
mistune==0.8.3
nbconvert==5.3.1
nbformat==4.4.0
notebook==5.3.1
numpy==1.14.0
pandas==0.22.0
pandocfilters==1.4.2
pathlib2==2.3.0
pefile==2017.11.5
pexpect==4.3.1
pickleshare==0.7.4
prompt-toolkit==1.0.15
ptyprocess==0.5.2
Pygments==2.2.0
PyInstaller==3.3.1
pyparsing==2.2.0
python-dateutil==2.6.1
pytz==2017.3
PyYAML==3.12
pyzmq==16.0.3
qtconsole==4.3.1
scandir==1.6
Send2Trash==1.4.2
simplegeneric==0.8.1
singledispatch==3.4.0.3
six==1.11.0
subprocess32==3.2.7
terminado==0.8.1
testpath==0.3.1
tornado==4.5.3
traitlets==4.3.2
wcwidth==0.1.7
webencodings==0.5.1
widgetsnbextension==3.1.0

Did anyone succeed to run jupyter notebook under PyInstaller? Any comment/help is more than welcome.

Thanks a lot, Greg

gdw2 commented 6 years ago

I haven't tried it myself, but there is this: https://medium.com/@m.mitchell_35295/jupyter-notebook-pyinstaller-4cb3482609aa

RufusVS commented 5 years ago

Looking at the log, you've got two 404 errors getting .js files. Could that be the problem? Then you see the Kernel Restarter try 4 more times and I guess it quits then. I can't help you reconfiguring your spec file or app to fix it though. Sorry.

web-sys1 commented 4 years ago

unfortunately not.

web-sys1 commented 4 years ago

Did you forget to place #!/usr/bin/env python3 in the script?

designerguy13-photonics commented 4 years ago

Hii actually i also was struggling this issue but than i ended up solving this issue as follows:

  1. Created a Miniconda environment for my PyMeep setup (Meep here is a python based simulation environment by MIT)
  2. I created the environment with a name mp
  3. Then going to that directory n used

Conda activate mp Jupyter notebook --allow-root

And it worked for me

Note: before doing all this i had to update and then upgrate the apt-get distribution