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')