Scipy和Cython时发生AttributeError,同时使用cx_Freeze将Python转换为exe [英] AttributeError with Scipy and Cython while converting Python to exe with cx_Freeze

查看:54
本文介绍了Scipy和Cython时发生AttributeError,同时使用cx_Freeze将Python转换为exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cx_Freeze将python脚本转换为exe文件.不幸的是,创建该exe文件后,执行该文件时出现 AttributeError :

I'm converting my python script to an exe-file using cx_Freeze. Unfortunately, I get an AttributeError while executing the exe-file after creating it:

AttributeError:类型对象'scipy.interpolate.interpnd.array'没有属性'__reduce_cython __'

AttributeError: type object 'scipy.interpolate.interpnd.array' has no attribute '__reduce_cython__'

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

Used Versions: 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.

我使用了以下cx_Freeze setup.py :

I used the following 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")]
      )

我希望exe文件能够运行并给出预测(欺诈检测),但我收到此错误消息(每次启动exe文件时):

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):

更新了错误文件

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. 正如@ead和@DavidW所评论的那样,您发布的错误消息表明,根据路径中的 Python36 文件夹,Scipy是从Python 3.6安装中导入的

  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

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__)

到您的 setup.py 文件中,以查看cx_Freeze构建可执行文件时实际使用的是哪个版本的Python和Scipy.

to your setup.py file to see which version of Python and Scipy are actually used when cx_Freeze builds the executable.

在Cython github存储库中对相同的错误消息进行了广泛的讨论,请参见

There is a quite extensive discussion of the same error message on the Cython github repository, see Issue #1953. You might find further advice there.

根据此讨论,该错误是由Cython中的一个问题引起的,该问题已在Cython 0.28中得以解决.您还会在其中找到以下有用的评论

According to this discussion, the error is caused by an issue in Cython which has been solved in Cython 0.28. You'll also find the following useful comment there:

[...]如果您安装的某个软件包是使用具有错误的旧Cython版本构建的,则仅在您的系统上安装Cython版本x.y完全无关紧要.

[...] just having Cython version x.y 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.

由于原因固定为0.28,我现在将关闭这张票.如果您发现使用0.28或更高版本构建的软件中发生了类似的问题,请打开一张新票.

I'll close this ticket now, since the cause was fixed with 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.

要验证Cython实现的软件包是否使用正确的Cython版本构建,请解压缩其源代码分发包(来自PyPI的 *.tar.gz ),然后找到.c .cpp 文件,并查看它们的第一行.如果显示/*由Cython 0.28 */或更高版本生成,则包含此修复程序.如果版本较旧,则不包含此修复程序,在这种情况下,最好向项目要求新版本.

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. If it says /* Generated by Cython 0.28 */ or a later version, it includes the fix. 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.

因此,您还需要检查所有使用Cython生成并包含在可执行文件中的软件包是否已使用Cython 0.28或更高版本进行了构建.

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.

这篇关于Scipy和Cython时发生AttributeError,同时使用cx_Freeze将Python转换为exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆