Traceback (most recent call last): File "E:\code_me\unittest\unittest_code.py", line 16, in <module> class TestCase(unittest.TestCase): AttributeError: module 'unittest' has no attribute 'TestCase'
时间: 2024-02-19 08:00:12 浏览: 70
这个错误通常是因为 Python 解释器无法找到 `unittest` 模块中的 `TestCase` 类。有几个可能的原因:
1. Python 安装没有完全完成,缺少测试模块。
2. 在代码中,可能意外地导入了名为 `unittest` 的其他模块,这会覆盖标准库中的 `unittest` 模块。
3. Python 版本不兼容。
你可以尝试在命令行输入 `python -m unittest` 看看是否会有相同的错误。如果出现错误,那么 Python 安装可能出了问题。如果没有错误,你可以检查代码中是否导入了其他名为 `unittest` 的模块,并尝试升级 Python 版本。
相关问题
AttributeError Traceback (most recent call last) Cell In[37], line 36 34 #%% 35 Pano_len = len(Pano_set_org) ---> 36 path_mat = np.zeros([Pano_len,Pano_len]).astype(np.bool) 37 for ind in list(Pano_set_org): 38 match_all_pd_start = match_all_pd_current[match_all_pd_current.pic_id1 == ind] File C:\Anaconda3\envs\pytorch\lib\site-packages\numpy\__init__.py:305, in __getattr__(attr) 300 warnings.warn( 301 f"In the future `np.{attr}` will be defined as the " 302 "corresponding NumPy scalar.", FutureWarning, stacklevel=2) 304 if attr in __former_attrs__: --> 305 raise AttributeError(__former_attrs__[attr]) 307 # Importing Tester requires importing all of UnitTest which is not a 308 # cheap import Since it is mainly used in test suits, we lazy import it 309 # here to save on the order of 10 ms of import time for most users 310 # 311 # The previous way Tester was imported also had a side effect of adding 312 # the full `numpy.testing` namespace 313 if attr == 'testing': AttributeError: module 'numpy' has no attribute 'bool'. `np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
这个错误是因为你使用了已经被弃用的np.bool,你需要将其改为Python内置的bool类型。你可以将代码中的以下语句:
```
path_mat = np.zeros([Pano_len,Pano_len]).astype(np.bool)
```
改为:
```
path_mat = np.zeros([Pano_len,Pano_len], dtype=bool)
```
这样就可以避免这个错误了。
阅读全文