是否可以生成 jupyter-notebook 的可执行文件 (.exe)?

Med*_*Med 8 python executable exe jupyter-notebook

我使用 jupyter notebook 在 python 中编写了一个代码,我想生成该程序的可执行文件。

luc*_*gcb 12

不,但是可以从 生成.py脚本.ipynb,然后可以其转换为 .exe

使用jupyter nbconvert (如果您使用的是 Anaconda,这已经包含在内)

在环境中:

pip install nbconvert
jupyter nbconvert --to script my_notebook.ipynb
Run Code Online (Sandbox Code Playgroud)

将生成一个my_notebook.py.

然后使用Pyinstaller

pip install pyinstaller
pyinstaller my_notebook.py
Run Code Online (Sandbox Code Playgroud)

您现在应该my_notebook.exe在您的文件夹中有 a和 dist 文件。

来源:一篇关于此的稍微过时的Medium 文章


ycx*_*ycx 9

您可以使用我编写的这段代码将大量.ipynb文件转换为.py文件。

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")
Run Code Online (Sandbox Code Playgroud)

.ipynb文件转换为.py文件后。
尝试运行.py文件以确保它们正常工作。之后,在终端或命令提示符中使用 Pyinstaller。 cd到您的.py文件位置。然后输入

pyinstaller --onefile yourfile.py
Run Code Online (Sandbox Code Playgroud)

这将生成一个单文件.exe程序