是否可以生成 jupyter-notebook 的可执行文件 (.exe)?
Is it possible to generate an executable (.exe) of a jupyter-notebook?
我使用 jupyter notebook 在 python 中编写了代码,我想生成该程序的可执行文件。
不,但是可以从.ipynb
生成.py
脚本,它可以然后转换为 .exe
与jupyter nbconvert
(如果你使用的是 Anaconda,这已经包含在内)
在环境中:
pip install nbconvert
jupyter nbconvert --to script my_notebook.ipynb
会生成一个my_notebook.py
.
然后 Pyinstaller :
pip install pyinstaller
pyinstaller my_notebook.py
您的文件夹中现在应该有 my_notebook.exe
和 dist 文件。
来源:有点过时Medium Article about this
您可以使用我编写的这段代码将大量 .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
程序
我使用 jupyter notebook 在 python 中编写了代码,我想生成该程序的可执行文件。
不,但是可以从.ipynb
生成.py
脚本,它可以然后转换为 .exe
与jupyter nbconvert (如果你使用的是 Anaconda,这已经包含在内)
在环境中:
pip install nbconvert
jupyter nbconvert --to script my_notebook.ipynb
会生成一个my_notebook.py
.
然后 Pyinstaller :
pip install pyinstaller
pyinstaller my_notebook.py
您的文件夹中现在应该有 my_notebook.exe
和 dist 文件。
来源:有点过时Medium Article about this
您可以使用我编写的这段代码将大量 .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
程序