Cython compile error “is not a valid module name”

你离开我真会死。 提交于 2020-01-05 06:42:13

问题


I trying to compile on windows a Cython file (.pyx), a file which I just saved from .py. Here is my project dir path.

c:\..\Project\App\Analyzer\
_init_.py
Few_other_files.py
consolidated_loop_C.pyx
cl_setup.py

Here is my cl_setup.py

from Cython.Build import cythonize
try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension


setup(
    name = "Consolidated Loop",
    ext_modules = cythonize("consolidated_loop_C.pyx")
)

I am using below statement for compling in the same folder.

python cl_setup.py build_ext --inplace

But I am getting the below error. my guess is I am missing certain parameters to cythonize(), tried to research without any luck.


回答1:


First, change your setup.py file to only use distutils

from Cython.Build import cythonize
from distutils.core import setup, Extension

setup(
    name = "Consolidated Loop",
    ext_modules = cythonize("consolidated_loop_C.pyx")
)

This is to facilitate the debugging for potential repliers.

Then, from a few experiments and other SO posts Python building cython extension with setup creates subfolder when __init__.py exists and The command `python setup.py build_ext --inplace` always create a new directory

I suggest to either move your cython file in a subdirectory or remove the __init__.py file. The latter issue very probably causes Python or Cython to guess the name of the module of the current directory, hence the dash issue. Also, setup.py files cannot live in the directory of the module and that will cause trouble.

If you intend to distribute or package your code, the former option (moving cleanly the files in a subdirectory with its own __init__.py, etc) is preferable. Else, just remove __init__.py and be done. This will create, with build_ext --inplace, a locally available Python module consolidated_loop_C.so.



来源:https://stackoverflow.com/questions/42102656/cython-compile-error-is-not-a-valid-module-name

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