是否可以在jupyter笔记本中生成可执行文件(.exe)?

时间:2019-04-18 08:03:27

标签: python jupyter-notebook exe executable

我使用jupyter笔记本用python写了一个代码,我想生成该程序的可执行文件。

2 个答案:

答案 0 :(得分:1)

否,但是可以从new Date(value)生成.py脚本,然后可以然后将其转换为.exe

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

在环境中:

.ipynb

将生成一个pip install nbconvert jupyter nbconvert --to script my_notebook.ipynb

然后使用Pyinstaller

my_notebook.py

您现在应该在文件夹中有一个pip install pyinstaller pyinstaller my_notebook.py 和dist文件。

来源:有点过时的Medium Article about this

答案 1 :(得分:0)

您可以使用我编写的这段代码将大量.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")

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

pyinstaller --onefile yourfile.py

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

相关问题