Search code examples
pythonjupyter-notebookexeexecutable

Is it possible to generate an executable (.exe) of a jupyter-notebook?


I wrote a code in python using jupyter notebook and i want to generate an executable of the program.


Solution

  • You can use this code I've written to convert large numbers of .ipynb files into .py files.

    srcFolder = r'input_folderpath_here'
    desFolder = r'output_folderpath_here'
    
    import os
    import nbformat
    from nbconvert import PythonExporter
    
    def convertNotebook(notebookPath, modulePath):
        with open(notebookPath) as fh:
            nb = nbformat.reads(fh.read(), nbformat.NO_CONVERT)
        exporter = PythonExporter()
        source, meta = exporter.from_notebook_node(nb)
        with open(modulePath, 'w+') as fh:
            fh.writelines(source)
    
    # For folder creation if doesn't exist
    if not os.path.exists(desFolder):
        os.makedirs(desFolder)
    
    for file in os.listdir(srcFolder):
        if os.path.isdir(srcFolder + '\\' + file):
            continue
        if ".ipynb" in file:
            convertNotebook(srcFolder + '\\' + file, desFolder + '\\' + file[:-5] + "py")
    

    Once you have converted your .ipynb files into .py files.
    Try running the .py files to ensure they work. After which, use Pyinstaller in your terminal or command prompt. cd to your .py file location. And then type

    pyinstaller --onefile yourfile.py
    

    This will generate a single file .exe program