简体   繁体   中英

Cython compile error "Cannot assign type 'double' to 'int'" using mingw64 in windows but ok in linux

I have problems installing a python package traj-dist https://github.com/bguillouet/traj-dist which uses Cython . It can be installed in Linux after comipling using gcc, but cannot in winodws using mingw64 gcc.

I use Python 3.8, Cython 0.29.21.

python setup.py build_ext --inplace --force

The full output error message is HERE

Error compiling Cython file:
------------------------------------------------------------
...
    q=len(Q)

    cc=_compute_critical_values(P,Q,p,q)
    eps=cc[0]
    while(len(cc)!=1):
        m_i=len(cc)/2-1
                    ^
------------------------------------------------------------

traj_dist\cydist\frechet.pyx:535:21: Cannot assign type 'double' to 'int'

The file that has compiling error is https://github.com/bguillouet/traj-dist/blob/master/traj_dist/cydist/frechet.pyx

How can I compile it in windows?

I think you're using different Cython versions on Windows and Linux. Here's a simplified example:

def f():
    cdef int a = 3/2

The Python 3 behaviour would be to return 1.5 while the Python 2 behaviour would be to return 1.

Cython can emulate both Python 2 and Python 3. See this answer for extensive details. For Cython 0.29.x it will emulate Python 2 by default (but give you a warning that the level should be set explicitly) and thus the file will compile. For Cython 3.0alpha (and higher) - which I suspect you have on Windows - it'll emulate Py3 by default (with a small exception for strings ).

When following Py3 behaviour Cython isn't happy assigning a C double to a C integer because not all double values will fit in an integer.

There's two changes you can make that'd make it compile. You only need to make one of these

  1. use // instead of / for force integer division.
  2. Set the Cython language level to 2 either in setup.py or at the top of a file with # cython: language_level=2

You should also file a bug report with the maintainers of the package telling them that the package will be broken by the up-coming Cython 3.0 release and that they can fix it with one of these methods.

What version of Python are you using? Looks like >= 3.6 is required. I don't know exactly which version this change started in, but integer math changed from Python 2.7 to Python 3.6.

For example in Python 2.7:

>>> 4/3
1

In Python 3.6:

>>> 4/3
1.3333333333333333

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