4

I have attempted to use the answer here to add the building of a cython extension into my package. It currently cythonizes the code to produce a .c file from the .pyx file but doesn't create a shared object .so file, as such when I try to import the package, and one of the modules attempts to import the shared object file it cannot find it.

My setup.py file (this is slightly cut-down) is like so:

from setuptools import setup
from setuptools.extension import Extension
import os
import numpy
from Cython.Build import cythonize

mypackage_root_dir = os.path.dirname(__file__)
with open(os.path.join(mypackage_root_dir, 'requirements.txt')) as requirements_file:
    requirements = requirements_file.read().splitlines()

extensions = [Extension(
    name="package.submodule.foo",
    sources=["package/submodule/foo.pyx"],
    include_dirs=[numpy.get_include()],
    )
]

setup(name='package',
      version=0.1,
      description='...',
      author='my name',
      author_email='my email',
      url="...",

      include_package_data=True,
      packages=['package',
                'package.submodule1',
                'package.submodule2',
                'package.submodule', # the one that uses the pyx file
      ],
      ext_modules = cythonize(extensions),
      install_requires=requirements,
)

How can I fix this such that I can get the setup.py file to build the shared object file when python setup.py install is run?

SomeRandomPhysicist
  • 1,531
  • 4
  • 19
  • 42
  • How do you call this `setup` file? – hpaulj Jul 02 '17 at 20:49
  • with `python setup.py install` – SomeRandomPhysicist Jul 02 '17 at 20:53
  • I'm not an expert on `cython`, but my scripts require `python setup.py build_ext --inplace`. This is with fairly standard scripts from the turtorials. – hpaulj Jul 02 '17 at 21:12
  • yeah, that it was I was doing when I was using it as a single cython file so that it could be built into a shared object file and then imported into python but I now want it to build and be installed along with the rest of my python package `python setup.py build_ext --inplace` does create the `.so` file. However when I run `python setup.py install` the submodule still cannot find shared object file, suggesting it is not copied to the install location and is not aware of the cython object. – SomeRandomPhysicist Jul 02 '17 at 21:19
  • Does each package have `__init__.py` file in the corresponding directory? – J.J. Hakala Jul 02 '17 at 23:17
  • yeah, each module has an init file and has been working as a package fine for a while. It's been installed on several different computers and the package is also on pip and installs fine from there with all modules. I just can't work out how to have it build and install the cython file such that the submodule can import it. – SomeRandomPhysicist Jul 03 '17 at 06:13

1 Answers1

1

The code in my question was working and building the .so file inside the package/subpackage directory in the install location, however, when I tried to import the package it couldn't find the file. However when I manually moved the file to the location of the root install directory of the package it worked.

It appears to therefore require that the shared object file be in the root directory of the package rather than the submodule directory.

I am able to achieve this by changing the extension definition like so:

extensions = [Extension(
        name="foo",
        sources=["package/submodule/foo.pyx"],
        include_dirs=[numpy.get_include()],
        )
    ]

This puts the .so file in the root install directory.

However I'm not sure why it requires this shared object file to be in the root of the package rather than the subpackage directory as is the case with normal python files.

SomeRandomPhysicist
  • 1,531
  • 4
  • 19
  • 42