Cython AttributeError: 'module' object has no attribute 'declare'

丶灬走出姿态 提交于 2019-12-13 07:09:06

问题


Compiling .pyx files was working fine but suddenly it started raising error when I type:

python setup.py build_ext --inplace

I get the error:

c:\Python27\cython\helloworld>python setup.py build_ext --inplace
running build_ext
cythoning hello.pyx to hello.c
Traceback (most recent call last):
  File "setup.py", line 8, in <module>
    ext_modules = [Extension("hello", ["hello.pyx"])]
  File "C:\Python27\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\site-packages\Cython\Distutils\build_ext.py", line 163,
in run
    _build_ext.build_ext.run(self)
  File "C:\Python27\lib\distutils\command\build_ext.py", line 340, in run
    self.build_extensions()
  File "C:\Python27\lib\site-packages\Cython\Distutils\build_ext.py", line 170,
in build_extensions
    ext.sources = self.cython_sources(ext.sources, ext)
  File "C:\Python27\lib\site-packages\Cython\Distutils\build_ext.py", line 317,
in cython_sources
    full_module_name=module_name)
  File "C:\Python27\lib\site-packages\Cython\Compiler\Main.py", line 605, in com
pile
    return compile_single(source, options, full_module_name)
  File "C:\Python27\lib\site-packages\Cython\Compiler\Main.py", line 546, in com
pile_single
    return run_pipeline(source, options, full_module_name)
  File "C:\Python27\lib\site-packages\Cython\Compiler\Main.py", line 386, in run
_pipeline
    import Pipeline
  File "C:\Python27\lib\site-packages\Cython\Compiler\Pipeline.py", line 7, in <
module>
    from Visitor import CythonTransform
  File "Visitor.py", line 9, in init Cython.Compiler.Visitor (Cython\Compiler\Vi
sitor.c:12838)
  File "C:\Python27\lib\site-packages\Cython\Compiler\Nodes.py", line 6, in <mod
ule>
    cython.declare(sys=object, os=object, copy=object,
AttributeError: 'module' object has no attribute 'declare'

I tried running it on a simple .pyx file helloworld:

def say_hello_to(name):
    print "Hello %s!" %name

with the setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = [Extension("hello", ["hello.pyx"])]
)

It already worked several times, something has changed but as I Googled and searched the issue I couldn't find any information. I'm using Python 2.7 64 bits, latest version of Cython and to compile the file I use Windows SDK C/C++ compiler and prepare the environment by running:

C:\Program Files\Microsoft SDKs\Windows\v7.0>set DISTUTILS_USE_SDK=1
C:\Program Files\Microsoft SDKs\Windows\v7.0>setenv /x64 /release

Any ideas of what might have happened? Tnx in advance!


回答1:


I have had the same error. Worked on it for one whole day until I have realized that I have followed some blog entry to create a cython.py file in my directory holding some information how to compile. I do not recall where I found this but by removing this file everything works fine




回答2:


I got the same error when running from a python script:

os.system("python setup.py build_ext --inplace")

while

python setup.py build_ext --inplace

worked from command promt.

The reason was that the name of my test package was "cython"... stupid me




回答3:


I'm not sure this helps, but when I got this error I deleted all .pyc files and ran the script again via IDLE (opened in IDLE and clicked F5) and then it worked just fine.



来源:https://stackoverflow.com/questions/13383567/cython-attributeerror-module-object-has-no-attribute-declare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!