0

This is an issue that comes up but I am yet to find a good resolution for it. So, I have a python project and I want the users to be able to install it in a minimal anaconda virtual environment.

My code also uses Cython for speeding up some compute intense functions and require the numpy headers to be able to cythonize them. So, I have a setup python script as follows:

from distutils.extension import Extension
from setuptools import setup, Extension


def create_extension():
    import numpy as np  # No luck even though I keep it at function scope
    return Extension("speed",
                     sources=["app/perf/booster.pyx"],
                     include_dirs=[np.get_include()],  # problematic
                     language="c++",
                     libraries=[],
                     extra_link_args=[])

setup(
    setup_requires=[
        'setuptools>=18.0',
        'numpy==1.13.3',
        'cython==0.28.2'
    ],

    ext_modules=[
        create_extension(),
    ],

......
install_requires=[
    'numpy==1.13.3',
    'Cython==0.28.2',
    'nibabel==2.2.1',
    'scipy==1.0.0'
],
)

However, when I run the build or install, the numpy inport fails. Although I have added it to setup_requires group and also the install_requires group, it seems to have no effect.

I can, of course, ask the users to install numpy beforehand but I would rather have it working in one single step and I was wondering if there is a way to achieve this.

After the comment, I tried the solution in that thread as follows:

f

rom distutils.extension import Extension
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools import setup, Extension

class build_ext(_build_ext):
    def finalize_options(self):
        _build_ext.finalize_options(self)
        # Prevent numpy from thinking it is still in its setup process:
        __builtins__.__NUMPY_SETUP__ = False
        import numpy
        self.include_dirs.append(numpy.get_include())

def create_extension():
    #import numpy as np
    return Extension("speed",
                     sources=["app/perf/booster.pyx"],
                     #include_dirs=[np.get_include()],
                     language="c++",
                     libraries=[],
                     extra_link_args=[])


setup(
    cmdclass={'build_ext': build_ext},
     .....

Now this fails with the error:

error: unknown file type '.pyx' (from 'app/perf/booster.pyx')
Luca
  • 10,458
  • 24
  • 107
  • 234
  • Possible duplicate of [How to Bootstrap numpy installation in setup.py](https://stackoverflow.com/questions/19919905/how-to-bootstrap-numpy-installation-in-setup-py) – user545424 Jul 17 '18 at 20:35
  • Can you verify that the highest voted answer in the question I linked works? I asked that question a long time ago, but unfortunately never got the chance to test it. If it does work, I will go ahead and mark that as the correct answer. – user545424 Jul 17 '18 at 20:39
  • @user545424 Thanks for that pointer. I did try it, please see my edits. I perhaps did not understand that solution correctly? – Luca Jul 17 '18 at 21:18
  • Possible duplicate of [Why doesn't setup\_requires work properly for numpy?](https://stackoverflow.com/questions/21605927/why-doesnt-setup-requires-work-properly-for-numpy) – Kefeng91 Jul 17 '18 at 22:09

0 Answers0