gpt4 book ai didi

Python:避免 NumPy 的 unittest.py 与本地 unittest.py 的冲突

转载 作者:太空宇宙 更新时间:2023-11-04 06:23:34 27 4
gpt4 key购买 nike

有没有人遇到过这样的情况,他们将自己代码的单元测试写到一个名为unittest.py的文件中,发现它与NumPy的unittest.py模块冲突?换句话说,如果我将其写入本地目录中的 unittest.py:

if __name__ == "__main__":
print "pre-import"
#import numpy
print "post-import"

然后(这里没有惊喜):

% python unittest.py
pre-import
post-import

但如果我这样做:

if __name__ == "__main__":
print "pre-import"
import numpy
print "post-import"

我得到:

% python unittest.py
pre-import
Traceback (most recent call last):
File "unittest.py", line 3, in <module>
import numpy
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/core/__init__.py", line 40, in <module>
from numpy.testing import Tester
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/testing/__init__.py", line 8, in <module>
from unittest import TestCase
ImportError: cannot import name TestCase

随后,iPython 加载失败:

% ipython
WARNING: IPython History requires SQLite, your history will not be saved
Traceback (most recent call last):
File "/home/jbbrown/local_bin/python/bin/ipython", line 7, in <module>
launch_new_instance()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 402, in launch_new_instance
app.initialize()
File "<string>", line 2, in initialize
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
return method(app, *args, **kwargs)
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 312, in initialize
self.init_shell()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 332, in init_shell
ipython_dir=self.ipython_dir)
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/config/configurable.py", line 318, in instance
inst = cls(*args, **kwargs)
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/interactiveshell.py", line 183, in __init__
user_module=user_module, custom_exceptions=custom_exceptions
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 478, in __init__
self.init_reload_doctest()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 662, in init_reload_doctest
doctest_reload()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/utils/doctestreload.py", line 72, in doctest_reload
import doctest
File "/home/jbbrown/local_bin/python/lib/python2.7/doctest.py", line 2118, in <module>
class DocTestCase(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'

If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
c.Application.verbose_crash=True

有趣的是,如果我将 import 语句移到 "if __name__" 套件之外,例如,

print "pre-import"
import numpy
print "post-import"
if __name__ == "__main__":
pass

我得到:

% python unittest.py
pre-import
pre-import
post-import
Traceback (most recent call last):
File "unittest.py", line 2, in <module>
import numpy
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/core/__init__.py", line 40, in <module>
from numpy.testing import Tester
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/testing/__init__.py", line 8, in <module>
from unittest import TestCase
ImportError: cannot import name TestCase

当然,更改包含我的单元测试的文件的名称并避免这种情况是微不足道的,但我想知道是否有其他人遇到过这种情况并想到了一种不包括重命名他们的单元测试文件的优雅解决方法?

最佳答案

您可以切换到absolute imports默认情况下,在 2.7 以上的 Python 版本上:

from __future__ import absolute_import

if __name__ == "__main__":
print "pre-import"
import numpy
print "post-import"

关于Python:避免 NumPy 的 unittest.py 与本地 unittest.py 的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10122151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com