Skip to content

Commit 5a027f0

Browse files
committed
Allow setup.py/pip to run query commands without importing numpy.
pip downloads and queries packages before building them. If you are running in a clean environment (e.g. a virtualenv) then numpy may not be installed, and the query command (egg_info) could fail saying that numpy cannot be imported. Solution is to check whether command is a query and only import numpy if it is not. Idea taken from similar commit in netcdf-python: Unidata/netcdf4-python@18f21a2 Should allow pip to install basemap from a requirements.txt file. Bonus, add requirements for numpy>=1.2.1 and matplotlib>=1.0.0, both taken from the basemap docs.
1 parent ee6a2f7 commit 5a027f0

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

setup.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
import sys, glob, os, numpy, subprocess
1+
import sys, glob, os, subprocess
2+
23
major, minor1, minor2, s, tmp = sys.version_info
34
if major==2 and minor1<4 or major<2:
45
raise SystemExit("""matplotlib and the basemap toolkit require Python 2.4 or later.""")
5-
from numpy.distutils.core import setup, Extension
6+
7+
from distutils.core import setup, Extension
8+
from distutils.dist import Distribution
69
from distutils.util import convert_path
710
from distutils import ccompiler, sysconfig
11+
12+
# Do not require numpy for just querying the package
13+
# Taken from the netcdf-python setup file (which took it from h5py setup file).
14+
inc_dirs = []
15+
if any('--' + opt in sys.argv for opt in Distribution.display_option_names +
16+
['help-commands', 'help']) or sys.argv[1] == 'egg_info':
17+
pass
18+
else:
19+
# append numpy include dir.
20+
import numpy
21+
inc_dirs.append(numpy.get_include())
22+
823
try:
924
from distutils.command.build_py import build_py_2to3 as build_py
1025
except ImportError:
@@ -62,7 +77,7 @@ def checkversion(GEOS_dir):
6277
manually and set the variable GEOS_dir (right after the line
6378
that says "set GEOS_dir manually here".""" % "', '".join(geos_search_locations))
6479
else:
65-
geos_include_dirs=[os.path.join(GEOS_dir,'include'),numpy.get_include()]
80+
geos_include_dirs=[os.path.join(GEOS_dir,'include'),inc_dirs]
6681
geos_library_dirs=[os.path.join(GEOS_dir,'lib'),os.path.join(GEOS_dir,'lib64')]
6782

6883
# proj4 and geos extensions.
@@ -130,6 +145,7 @@ def checkversion(GEOS_dir):
130145
download_url = "https://downloads.sourceforge.net/project/matplotlib/matplotlib-toolkits/basemap-{0}/basemap-{0}.tar.gz".format(__version__),
131146
author = "Jeff Whitaker",
132147
author_email = "[email protected]",
148+
install_requires = ["numpy>=1.2.1", "matplotlib>=1.0.0"],
133149
platforms = ["any"],
134150
license = "OSI Approved",
135151
keywords = ["python","plotting","plots","graphs","charts","GIS","mapping","map projections","maps"],

0 commit comments

Comments
 (0)