PyRaider
PyRaider

Reputation: 689

Cython compile error "is not a valid module name"

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.

enter image description here

Upvotes: 2

Views: 9347

Answers (2)

user2058374
user2058374

Reputation: 77

hyphen '-' in your file name might cause this error. To solve this error, I renamed - to _.

Upvotes: 2

Pierre de Buyl
Pierre de Buyl

Reputation: 7293

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.

Upvotes: 6

Related Questions