【问题标题】:Creating an executable Python创建可执行的 Python
【发布时间】:2016-03-27 01:21:23
【问题描述】:

我刚刚完成了第一次使用 python 的工作,我想创建一个可执行文件来分发。但是,我很难做到这一点。我已经尝试过 py2exe、pyinstaller 和 cx_freeze,但都没有成功。搜索了很多意见,我相信 pyinstaller 是解决我问题的最佳解决方案,但我可以让它工作。我在我的程序中所做的导入如下:

import os
import pygtk
import gtk
import MySQLdb
import getpass as gt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.io import sql
from scipy import stats, integrate
from scipy.optimize import curve_fit
import seaborn as sns
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import train_test_split
from sklearn import metrics

阅读 pyinstaller 的文档后,我注意到它仅不适用于 libs scikitlearn (sklearn) 和 seaborn。当我使用 'pyinstaller --onefile Principal.py' 时,我得到了构建和 dist 粘贴,但是当我尝试运行 .exe 文件时,我得到以下消息:

C:\Users\Lucas\AppData\Local\Temp\_MEI82242\gtk\__init__.py:127: RuntimeWarning:
 PyOS_InputHook is not available for interactive use of PyGTK
Traceback (most recent call last):
  File "<string>", line 13, in <module>
  File "c:\users\lucas\appdata\local\temp\pip-build-6fpfyh\pyinstaller\PyInstall
er\loader\pyimod03_importers.py", line 363, in load_module
  File "c:\users\lucas\anaconda\lib\site-packages\pandas\__init__.py", line 13,
in <module>
    "extensions first.".format(module))
ImportError: C extension: lib not built. If you want to import pandas from the s
ource directory, you may need to run 'python setup.py build_ext --inplace' to bu
ild the C extensions first.
Principal returned -1

有人可以帮我解决我必须做的事情吗?

【问题讨论】:

  • 也许你可以看看使用 Docker?我不认为“将 python 打包为 exe”是一个非常受支持或推荐的工作流程
  • 您的应用需要在创建应用时重建 pandas 包。导入模块(熊猫)不可再分发。一些包需要编译。安装准备使用的库。如果没有源代码,则不能在 .exe 中使用。不是错误,缺少包(源码)!
  • 那你建议我做什么?我正在尝试创建一个可执行文件,以便稍后使用 Inno 创建一个安装程序(他要求提供一个 .exe 文件)。如果我使用 pyinstaller 创建给我的 exe 并在 Inno 中添加 pandas 和其他库的路径,我的程序应该可以工作吗?

标签: python scikit-learn exe pyinstaller seaborn


【解决方案1】:

您的问题是您的 python 无法构建基于 C 的包 (numpy)。可能是安装了一些库:

  • 在您的 Python 发行版 (anaconda) 中
  • 使用预编译包的 Wheel 安装文件 (.whl)

您可以尝试通过两种方式解决此问题:

  • 安装最新的 MS Visual C++ 编译器,以便将 vcvarsall.bat 用于 C 扩展
  • 安装最新的Visual Studio Community,其中包括一个IDE 和多种语言的编译器。第二个选项通常有效,但它需要超过 6 GB 的硬盘空间

安装完成后,您可以启动错误消息告诉您的内容:

python setup.py build_ext --inplace

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 2010-10-16
    • 2013-05-20
    • 2023-03-27
    • 1970-01-01
    • 2020-06-23
    • 2019-04-16
    • 2016-01-13
    相关资源
    最近更新 更多