Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 483
Med Avatar asked Apr 18 '19 08:04

Med


People also ask

Is it possible to generate an executable program from a Jupyter Notebook?

I wrote a code in python using jupyter notebook and i want to generate an executable of the program. Show activity on this post. No, however it is possible to generate a .py script from .ipynb, which can then be converted to a .exe

How do I create a Python program in Jupyter?

I wrote a Python program in Jupyter Notebook. The program uses libraries installed through Anaconda. I need to get a separate executable Python file that would work on forks of Ubuntu and Debian. I created the .py file from the .ipynb file through the menu in the Jupyter Notebook: File -> Download as -> Python (.py).

How to create an executable file in Python?

Two simple ways to create a Python executable file. 1 Making an Executable file with auto-py-to-exe#N#- Installing with pip#N#- Running auto-py-to-exe#N#- Step 1: Add the script... 2 Making an Executable file with PyInstaller More ...

How do I create an executable file using auto-Py-to-Exe?

Once you install auto-py-to-exe, making an executable file is as easy as writing the following command. After running the command, the following GUI application will open. I will walk you through each option to properly create an executable file. Browse the script you wish to convert and add it to the “Script Location” field.


2 Answers

No, however it is possible to generate a .py script from .ipynb, which can then be converted to a .exe

With jupyter nbconvert (If you are using Anaconda, this is already included)

In the environment :

pip install nbconvert
jupyter nbconvert --to script my_notebook.ipynb

Will generate a my_notebook.py.

Then with Pyinstaller :

pip install pyinstaller
pyinstaller my_notebook.py

You should now have a my_notebook.exe and dist files in your folder.

Source: A slightly outdated Medium Article about this

like image 138
lucasgcb Avatar answered Oct 26 '22 23:10

lucasgcb


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

like image 40
ycx Avatar answered Oct 27 '22 01:10

ycx