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

风格不统一 提交于 2020-02-03 01:55:30

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/55741607/is-it-possible-to-generate-an-executable-exe-in-a-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!