【发布时间】:2019-10-26 13:58:07
【问题描述】:
我需要推迟一些库的导入,除非需要它们,所以我创建了一个对象来推迟导入。
class Importer(object):
def __init__(self, name):
self.name = name
def __bool__(self):
try:
mod = importlib.import_module(self.name)
return True
except ImportError:
return False
def __getattr__(self, name):
try:
mod = importlib.import_module(self.name)
attr = getattr(mod, name)
setattr(self, name, attr)
return attr
except ImportError as e:
msg = self.name + " must be installed"
raise ImportError(msg)
我的测试已经运行nose,一切都通过了。但是,当我运行 --with-doctest 时,它会运行一个额外的测试(我尚未定义),该测试会在未安装给定库的任何环境中生成错误。
测试下面的代码会重现我遇到的问题。在没有numpy 的环境中,nosetests --with-doctest 正在运行我尚未定义的第二个测试。
numpy = Importer('numpy')
def mean(array):
"""
Mean of an array.
Examples
--------
>>> mean([1, 2, 3])
2.0
>>> mean([1, -1])
0.0
"""
if numpy:
return numpy.mean(array)
return float(sum(array) / len(array))
显然,只有一个 doctest 可供测试。此外,如果安装了numpy,则仅运行 1 个测试。那么为什么当没有安装numpy 时我要进行第二次测试呢?这是输出。
.E
======================================================================
ERROR: Failure: ImportError (numpy must be installed)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/devin/Dropbox/Devin_S_+_Spark_Wave/nimble/zissue/issue.py", line 16, in __getattr__
mod = importlib.import_module(self.name)
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'numpy'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/site-packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/site-packages/nose/plugins/manager.py", line 154, in generate
for r in result:
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/site-packages/nose/plugins/doctests.py", line 228, in loadTestsFromModule
tests = self.finder.find(module)
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/doctest.py", line 933, in find
self._find(tests, obj, name, module, source_lines, globs, {})
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/doctest.py", line 992, in _find
if ((inspect.isroutine(inspect.unwrap(val))
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/inspect.py", line 512, in unwrap
while _is_wrapper(func):
File "/Users/devin/anaconda3/envs/issueEnv/lib/python3.6/inspect.py", line 503, in _is_wrapper
return hasattr(f, '__wrapped__')
File "/Users/devin/Dropbox/testing/issue/issue.py", line 22, in __getattr__
raise ImportError(msg)
ImportError: numpy must be installed
----------------------------------------------------------------------
Ran 2 tests in 0.003s
【问题讨论】:
标签: python-3.x nose doctest