astropy/astropy — pull request #454 by iguananaut (2012-11-02 15:47:10+00:00)
Title: Check for '_pytest' in sys.modules before trying to import Astropy's pytest

Because if '_pytest' is already in sys.modules it's likely that we're already running the py.test command, and so we should use the already imported system pytest.

This came up because when running the system py.test, if it's an older version than the one we're using now strange errors can occur such as:

```
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/usr/stsci/pyssgdev/Python-2.7.3/lib/python2.7/site-packages/_pytest/main.py", line 68, in wrap_session
INTERNALERROR> config.pluginmanager.do_configure(config)
INTERNALERROR> File "/usr/stsci/pyssgdev/Python-2.7.3/lib/python2.7/site-packages/_pytest/core.py", line 267, in do_configure
INTERNALERROR> config.hook.pytest_configure(config=self._config)
INTERNALERROR> File "/usr/stsci/pyssgdev/Python-2.7.3/lib/python2.7/site-packages/_pytest/core.py", line 419, in __call__
INTERNALERROR> return self._docall(methods, kwargs)
INTERNALERROR> File "/usr/stsci/pyssgdev/Python-2.7.3/lib/python2.7/site-packages/_pytest/core.py", line 430, in _docall
INTERNALERROR> res = mc.execute()
INTERNALERROR> File "/usr/stsci/pyssgdev/Python-2.7.3/lib/python2.7/site-packages/_pytest/core.py", line 348, in execute
INTERNALERROR> res = method(**kwargs)
INTERNALERROR> File "/usr/stsci/pyssgdev/Python-2.7.3/lib/python2.7/site-packages/_pytest/assertion/__init__.py", line 45, in pytest_configure
INTERNALERROR> _load_modules(mode)
INTERNALERROR> File "/usr/stsci/pyssgdev/Python-2.7.3/lib/python2.7/site-packages/_pytest/assertion/__init__.py", line 98, in _load_modules
INTERNALERROR> from _pytest.assertion import reinterpret
INTERNALERROR> File "/bray/sc1/root/src/astropy/astropy/astropy/extern/pytest.py", line 2447, in load_module
INTERNALERROR> do_exec(co, module.__dict__)
INTERNALERROR> File "", line 1, in do_exec_def
INTERNALERROR> File "_pytest.assertion.reinterpret", line 3, in
INTERNALERROR> from _pytest.assertion.util import BuiltinAssertionError
INTERNALERROR> ImportError: cannot import name BuiltinAssertionError
```

This made even me a little frightened. If you set the `ASTROPY_USE_SYSTEM_PYTEST` environment variable everything works fine, but it's easy to miss that in the docs. I think this pull request makes a reasonable assumption...though I know this has been hashed out at length before, so maybe there's already a known good reason not to do this?