Skip to content

Commit c9a84c5

Browse files
committed
fix numpy related import error on fresh install
Signed-off-by: masadcv <muhammad.asad@kcl.ac.uk>
1 parent c910db5 commit c9a84c5

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

setup.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
import numpy
32
import setuptools
43
from distutils.core import setup
54
from distutils.extension import Extension
@@ -10,7 +9,7 @@
109
module_name1 = 'maxflow'
1110
maxflow_source = "maxflow_python/wrap_py{0:}.cpp".format(py_version)
1211
module1 = Extension(module_name1,
13-
include_dirs = [numpy.get_include(),'./dependency', './maxflow_python'],
12+
include_dirs = ['./dependency', './maxflow_python'],
1413
sources = ['maxflow_python/maxflow.cpp',
1514
'maxflow_python/util.cpp',
1615
'dependency/maxflow-v3.0/graph.cpp',
@@ -20,7 +19,7 @@
2019
module_name2 = 'denseCRF'
2120
densecrf_source = "densecrf_python/wrap2D_py{0:}.cpp".format(py_version)
2221
module2 = Extension(module_name2,
23-
include_dirs = [numpy.get_include(),
22+
include_dirs = [
2423
'./dependency/densecrf/include',
2524
'./dependency/densecrf/external/liblbfgs/include'],
2625
sources=['./densecrf_python/densecrf.cpp',
@@ -40,7 +39,7 @@
4039
module_name3 = 'denseCRF3D'
4140
densecrf_source = "densecrf_python/wrap3D_py{0:}.cpp".format(py_version)
4241
module3 = Extension(module_name3,
43-
include_dirs = [numpy.get_include(),
42+
include_dirs = [
4443
'./dependency/densecrf3d/include',
4544
'./dependency/densecrf/external/liblbfgs/include'],
4645
sources=['./densecrf_python/densecrf3d.cpp',
@@ -69,6 +68,20 @@
6968
with open('README.md', encoding='utf-8') as f:
7069
long_description = f.read()
7170

71+
from setuptools.command.build_ext import build_ext
72+
73+
class CustomBuildExtCommand(build_ext):
74+
"""build_ext command for use when numpy headers are needed."""
75+
def run(self):
76+
77+
# Import numpy here, only when headers are needed
78+
import numpy
79+
80+
# Add numpy headers to include_dirs
81+
self.include_dirs.append(numpy.get_include())
82+
83+
# Call original build_ext command
84+
build_ext.run(self)
7285

7386
setup(name=package_name,
7487
version = "0.1.1",
@@ -87,7 +100,10 @@
87100
'Programming Language :: Python :: 2',
88101
'Programming Language :: Python :: 3',
89102
],
90-
python_requires = '>=3.6')
103+
python_requires = '>=3.6',
104+
cmdclass = {'build_ext': CustomBuildExtCommand},
105+
install_requires=['numpy'],
106+
)
91107

92108

93109
# to build, run python stup.py build or python setup.py build_ext --inplace

0 commit comments

Comments
 (0)