24

I am building a package in Cython. I am using the following as the structure for setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy
import scipy

extensions = [
    Extension("xxxxx",["xxxx/xxxxx.pyx"],
    include_dirs=[numpy.get_include(),"."]),
    Extension("nnls",["xxxxx/xxxxx.pyx"],
              include_dirs=[numpy.get_include(),"."]),
]

setup(
    name='xxxxxx',
    version='0.0.0',
    description='''********''',
    url='xxxxxxx',
    author='xxxxx',
    author_email='xxxxx',
    packages=[
        'xxxxx',
    ],
    install_requires=[
        'cython',
        'numpy',
        'scipy',
    ],
    ext_modules=cythonize(extensions),
)

However, I am getting an error upon installation in Python 3. It is working in Python 2 however, it is not compiling in Python 3 having the following error:

dynamic module does not define module export function

How can I solve this problem? Is the structure of the setup.py the reason why this is not compiling?

linusg
  • 6,289
  • 4
  • 28
  • 78
Alger Remirata
  • 529
  • 1
  • 5
  • 17

3 Answers3

9

You need to call setup.py with Python 3 (python3 setup.py build_ext, maybe --inplace). It's because Python 3 defines a different name for the init function called when the module starts, and so you need to build it using Python 3 to ensure the correct name is generated.

See dynamic module does not define init function (PyInit_fuzzy) and How to specify Python 3 source in Cython's setup.py? for slightly more detail (it's bordering on a duplicate of these questions, but isn't quite in my view)

DavidW
  • 29,336
  • 6
  • 55
  • 86
6

I experienced this and found that I had to use the same name of .pyx as the module name, e.g.

makefile:

# (default)
# INSTALL_DIR:=/usr/lib/python3.6/site-packages
# (my venv)
INSTALL_DIR:=/home/<username>/python3_venv/lib/python3.6/site-packages
all:
    sudo python3 setup_myproj.py install --install-lib ${INSTALL_DIR}

setup_myproj.py

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

ext = Extension("myproj",
                sources=["myproj.pyx", "myCppProjFacade.cpp"],
                <etc>
                language="c++"
               )

setup(name="myproj",
      version="0.0.1",
      ext_modules=cythonize(ext))

client module, run after installing to venv

import myproj as myCppProjWrapper
...

I also found that if the "myproj" names are different, under <python-lib-dir>/<python-vers>/site-packages the .so and .egg-info names are different and the client fails to load it.

In addition I found that the client's environment does not need to have the cython package installed.

Den-Jason
  • 2,395
  • 1
  • 22
  • 17
0

I had the same error for torchvision. FIxed it by downgrading the installation versions:

pip install torch==1.2.0+cu92 torchvision==0.4.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39