简体   繁体   中英

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. Unfortunately, I get an AttributeError while executing the exe-file after creating it:

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

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.

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")]
      )

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

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

    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.

  2. 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.

    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:

    [...] 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.

    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.

    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. 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.

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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