Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit 0a2c8ff

Browse files
authored
Merge pull request #292 from ndawe/master
[MRG] numpy dependency in setup.py
2 parents a7bf740 + a449f36 commit 0a2c8ff

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

setup.py

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@
88
if sys.version_info < (2, 6):
99
sys.exit("root_numpy only supports python 2.6 and above")
1010

11-
# check that NumPy is installed
12-
try:
13-
import numpy
14-
except ImportError:
15-
raise RuntimeError(
16-
"numpy cannot be imported. numpy must be installed "
17-
"prior to installing root_numpy")
11+
if sys.version_info[0] < 3:
12+
import __builtin__ as builtins
13+
else:
14+
import builtins
1815

1916
try:
2017
# try to use setuptools if installed
2118
from pkg_resources import parse_version, get_distribution
22-
from setuptools import setup, Extension
19+
2320
if get_distribution('setuptools').parsed_version < parse_version('0.7'):
2421
# before merge with distribute
2522
raise ImportError
23+
24+
from setuptools import setup, Extension
25+
from setuptools.command.build_ext import build_ext as _build_ext
2626
except ImportError:
2727
# fall back on distutils
2828
from distutils.core import setup, Extension
29+
from distutils.command.build_ext import build_ext as _build_ext
2930

3031
import os
3132
from glob import glob
@@ -66,6 +67,27 @@
6667
"root-config is not in PATH and ROOTSYS is not set. "
6768
"Is ROOT installed correctly?")
6869

70+
class build_ext(_build_ext):
71+
def finalize_options(self):
72+
_build_ext.finalize_options(self)
73+
# prevent numpy from thinking it is still in its setup process:
74+
try:
75+
del builtins.__NUMPY_SETUP__
76+
except AttributeError:
77+
pass
78+
import numpy
79+
self.include_dirs.append(numpy.get_include())
80+
81+
config = {
82+
'ROOT_version': str(root_version),
83+
'numpy_version': numpy.__version__,
84+
}
85+
86+
# write config.json
87+
import json
88+
with open('root_numpy/config.json', 'w') as config_file:
89+
json.dump(config, config_file, indent=4)
90+
6991
librootnumpy = Extension(
7092
'root_numpy._librootnumpy',
7193
sources=[
@@ -74,7 +96,6 @@
7496
depends=glob('root_numpy/src/*.h'),
7597
language='c++',
7698
include_dirs=[
77-
numpy.get_include(),
7899
'root_numpy/src',
79100
],
80101
extra_compile_args=root_cflags + [
@@ -101,7 +122,6 @@
101122
],
102123
language='c++',
103124
include_dirs=[
104-
numpy.get_include(),
105125
'root_numpy/src',
106126
'root_numpy/tmva/src',
107127
],
@@ -138,15 +158,16 @@
138158
if install:
139159
print(__doc__)
140160

141-
config = {
142-
'ROOT_version': str(root_version),
143-
'numpy_version': numpy.__version__,
144-
}
145-
146-
# write config.json
147-
import json
148-
with open('root_numpy/config.json', 'w') as config_file:
149-
json.dump(config, config_file, indent=4)
161+
# Figure out whether to add ``*_requires = ['numpy']``.
162+
# We don't want to do that unconditionally, because we risk updating
163+
# an installed numpy which fails too often. Just if it's not installed, we
164+
# may give it a try.
165+
try:
166+
import numpy
167+
except ImportError:
168+
build_requires = ['numpy']
169+
else:
170+
build_requires = []
150171

151172
setup(
152173
name='root_numpy',
@@ -164,6 +185,12 @@
164185
'root_numpy': ['testdata/*.root', 'config.json'],
165186
},
166187
ext_modules=ext_modules,
188+
cmdclass={'build_ext': build_ext},
189+
setup_requires=build_requires,
190+
install_requires=build_requires,
191+
extras_require={
192+
'with-numpy': ('numpy',),
193+
},
167194
zip_safe=False,
168195
classifiers=[
169196
'Intended Audience :: Science/Research',

0 commit comments

Comments
 (0)