diff --git a/setup.py b/setup.py index e735026a..6c55c6d6 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,19 @@ import sys import setuptools from distutils.command.build_py import build_py +if sys.version_info[0] < 3: + import __builtin__ as builtins +else: + import builtins + +# This is a bit (!) hackish: we are setting a global variable so that the main +# skimage __init__ can detect if it is being loaded by the setup routine, to +# avoid attempting to load components that aren't built yet: +# the numpy distutils extensions that are used by scikit-image to recursively +# build the compiled extensions in sub-packages is based on the Python import +# machinery. +builtins.__SKIMAGE_SETUP__ = True + with open('skimage/__init__.py') as fid: for line in fid: diff --git a/skimage/__init__.py b/skimage/__init__.py index e9216950..2b152156 100644 --- a/skimage/__init__.py +++ b/skimage/__init__.py @@ -60,7 +60,7 @@ import os.path as osp import imp import functools import warnings -from .util.dtype import * +import sys pkg_dir = osp.abspath(osp.dirname(__file__)) data_dir = osp.join(pkg_dir, 'data') @@ -109,4 +109,51 @@ doctest.__doc__ = doctest.__doc__ doctest_verbose = functools.partial(test, doctest=True, verbose=True) doctest_verbose.__doc__ = doctest.__doc__ -del warnings, functools, osp, imp + +# Logic for checking for improper install and importing while in the source +# tree when package has not been installed inplace. +# Code adapted from scikit-learn's __check_build module. +_INPLACE_MSG = """ +It appears that you are importing a local scikit-image source tree. For +this, you need to have an inplace install. Maybe you are in the source +directory and you need to try from another location.""" + +_STANDARD_MSG = """ +Your install of scikit-image appears to be broken. +Try re-installing the package following the instructions at: +http://scikit-image.org/docs/stable/install.html """ + + +def _raise_build_error(e): + # Raise a comprehensible error + local_dir = osp.split(__file__)[0] + msg = _STANDARD_MSG + if local_dir == "skimage": + # Picking up the local install: this will work only if the + # install is an 'inplace build' + msg = _INPLACE_MSG + raise ImportError("""%s +It seems that scikit-image has not been built correctly. +%s""" % (e, msg)) + +try: + # This variable is injected in the __builtins__ by the build + # process. It used to enable importing subpackages of skimage when + # the binaries are not built + __SKIMAGE_SETUP__ +except NameError: + __SKIMAGE_SETUP__ = False + +if __SKIMAGE_SETUP__: + sys.stderr.write('Partial import of skimage during the build process.\n') + # We are not importing the rest of the scikit during the build + # process, as it may not be compiled yet +else: + try: + from ._shared import geometry + del geometry + except ImportError as e: + _raise_build_error(e) + from .util.dtype import * + +del warnings, functools, osp, imp, sys