简体   繁体   English

使用 cx_Freeze 将 Python 转换为 exe 时出现 Scipy 和 Cython 的 AttributeError

[英]AttributeError with Scipy and Cython while converting Python to exe with cx_Freeze

I'm converting my python script to an exe-file using cx_Freeze.我正在使用 cx_Freeze 将我的 python 脚本转换为 exe 文件。 Unfortunately, I get an AttributeError while executing the exe-file after creating it:不幸的是,我在创建 exe 文件后执行它时出现AttributeError

AttributeError: type object 'scipy.interpolate.interpnd.array' has no attribute '__reduce_cython__' AttributeError:类型对象“scipy.interpolate.interpnd.array”没有属性“__reduce_cython__”

Used Versions: Python: 3.7.3 Scipy: 1.2.1 Cython: 0.29.7 cx_Freeze: 5.1.1使用的版本:Python:3.7.3 Scipy:1.2.1 Cython:0.29.7 cx_Freeze:5.1.1

I already upgraded all the used modules to the newest versions and searched for the error.我已经将所有使用的模块升级到最新版本并搜索了错误。 I also tried to uninstall and re-install the modules again.我还尝试再次卸载并重新安装模块。

I used the following cx_Freeze setup.py :我使用了以下 cx_Freeze setup.py

from cx_Freeze import setup, Executable
import os

# Set environment variables
# https://stackoverflow.com/questions/35533803/keyerror-tcl-library-when-i-use-cx-freeze
os.environ['TCL_LIBRARY'] = r'C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\tcl\tk8.6'

includes = ["numpy", "numpy.core._methods", "numpy.lib.format", "sklearn", "ipaddress", "schwifty", "pandas", "multiprocessing.pool", "pkg_resources._vendor", "appdirs", "sklearn.ensemble.forest", "packaging.version", "packaging.specifiers", "packaging.requirements", "xgboost", "email.mime.text", "email.mime.multipart", "idna.idnadata", 'scipy._distributor_init', 'scipy.sparse.csgraph._validation', "cython", "scipy.interpolate.interpnd", "scipy"]

setup(name = "fraudDetection",
      version = "0.1",
      description = "",
      options = {'build_exe': {'includes': includes}},
      executables = [Executable("fraudDetection.py")]
      )

I expect the exe-file to run and give a prediction (fraud-detection) but I got this error message (every time I start the exe-file):我希望 exe 文件能够运行并给出预测(欺诈检测),但我收到此错误消息(每次我启动 exe 文件时):

Edit: Updated the error file编辑:更新了错误文件

File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "fraudDetection.py", line 40, in <module>
    from sklearn import preprocessing
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\preprocessing\__init__.py", line 6, in <module>
    from ._function_transformer import FunctionTransformer
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\preprocessing\_function_transformer.py", line 5, in <module>
    from ..utils.testing import assert_allclose_dense_sparse
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\utils\testing.py", line 21, in <module>
    import scipy.io
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\scipy\io\__init__.py", 
line 97, in <module>
    from .matlab import loadmat, savemat, whosmat, byteordercodes
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\scipy\io\matlab\__init__.py", line 13, in <module>
    from .mio import loadmat, savemat, whosmat
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\scipy\io\matlab\mio.py", 
line 10, in <module>
    from .miobase import get_matfile_version, docfiller
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\scipy\io\matlab\miobase.py", line 22, in <module>
    from scipy.misc import doccer
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\scipy\misc\__init__.py", 
line 68, in <module>
    from scipy.interpolate._pade import pade as _pade
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\scipy\interpolate\__init__.py", line 175, in <module>
    from .interpolate import *
  File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Anaconda3_64\lib\site-packages\scipy\interpolate\interpolate.py", line 32, in <module>
    from .interpnd import _ndim_coords_from_arrays
  File "stringsource", line 105, in init scipy.interpolate.interpnd
AttributeError: type object 'scipy.interpolate.interpnd.array' has no attribute '__reduce_cython__'
  1. As @ead and @DavidW commented, the error message you've posted indicates that Scipy gets imported from a Python 3.6 installation, in view of the Python36 folder in the path正如@ead 和@DavidW 评论的那样,鉴于路径中的Python36文件夹,您发布的错误消息表明 Scipy 是从 Python 3.6 安装导入的

    File "C:\\Users\\user.name\\AppData\\Roaming\\Python\\Python36\\site-packages\\scipy\\interpolate\\interpolate.py"

    But maybe you simply forgot to edit this part of the error message.但也许您只是忘记编辑错误消息的这一部分。

    Anyway, you could add the following lines无论如何,您可以添加以下几行

    import sys import scipy print(sys.version) print(scipy.__version__)

    to your setup.py file to see which version of Python and Scipy are actually used when cx_Freeze builds the executable.到您的setup.py文件以查看当 cx_Freeze 构建可执行文件时实际使用的 Python 和 Scipy 版本。

  2. There is a quite extensive discussion of the same error message on the Cython github repository, see Issue #1953 .在 Cython github 存储库上对相同的错误消息进行了相当广泛的讨论,请参阅问题 #1953 You might find further advice there.您可能会在那里找到进一步的建议。

    According to this discussion, the error is caused by an issue in Cython which has been solved in Cython 0.28.根据此讨论,该错误是由 Cython 中的一个问题引起的,该问题已在 Cython 0.28 中解决。 You'll also find the following useful comment there:您还会在那里找到以下有用的评论

    [...] just having Cython version xy installed on your system is entirely irrelevant if a certain package that you install was built with an older Cython version that has a bug. [...] 如果您安装的某个软件包是使用有错误的旧 Cython 版本构建的,那么在您的系统上安装 Cython 版本 xy 完全无关紧要。

    I'll close this ticket now, since the cause was fixed with 0.28.我现在将关闭这张票,因为原因已用 0.28 修复。 Please open a new ticket if you find a similar problem that occurs in software that was built with 0.28 or later.如果您发现使用 0.28 或更高版本构建的软件中出现类似问题,请开新票。

    Edit: to verify that a Cython implemented package was built with the correct(ed) Cython version, unpack its source distribution ( *.tar.gz from PyPI), find the .c or .cpp files in it and look at their first line.编辑:为了验证 Cython 实现的包是用正确的(ed)Cython 版本构建的,解压其源代码分发( *.tar.gz来自 PyPI),在其中找到.c.cpp文件并查看它们的第一行. If it says /* Generated by Cython 0.28 */ or a later version, it includes the fix.如果它显示/* Generated by Cython 0.28 */或更高版本,则它包含修复程序。 If the version is older, it does not include the fix, in which case it's best to ask the project for a new release.如果版本较旧,则不包含修复程序,在这种情况下,最好向项目索要新版本。

    So you also need to check that all packages built with Cython and included in your executable have been build with Cython 0.28 or a later version.因此,您还需要检查所有使用 Cython 构建并包含在您的可执行文件中的包是否都是使用 Cython 0.28 或更高版本构建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM