背景
根据这里所说,tensorboardX好像寄了,但是Pytorch官方给出了SummaryWriter作为替代品。
环境是Torch1.10.2+cu113,tensorboard2.8.0。
问题
试图使用如下命令
from torch.utils.tensorboard import SummaryWriter
导入tensorboard,报错:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Anaconda\envs\torchenv1100\lib\site-packages\torch\utils\tensorboard\__init__.py", line 4, in <module>
LooseVersion = distutils.version.LooseVersion
AttributeError: module 'distutils' has no attribute 'version'
问题分析
根据这里的描述,原因是setuptools包版本过高,需要降低版本。
这里给出另外一个解决方案。打开Anaconda\envs\torchenv1100\lib\site-packages\torch\utils\tensorboard_init_.py文件
import tensorboard
from setuptools import distutils
LooseVersion = distutils.version.LooseVersion
if not hasattr(tensorboard, '__version__') or LooseVersion(tensorboard.__version__) < LooseVersion('1.15'):
raise ImportError('TensorBoard logging requires TensorBoard version 1.15 or above')
del distutils
del LooseVersion
del tensorboard
from .writer import FileWriter, SummaryWriter # noqa: F401
from tensorboard.summary.writer.record_writer import RecordWriter # noqa: F401
其中,第4-7行代码的本意是确保tensorboard版本要高于1.15(含)。但是由于setuptools在60.0之后版本中不存在“distutils.version.LooseVersion”,因此这里报错了。文章来源:https://www.toymoban.com/news/detail-508455.html
解决方案
手动检查tensorboard版本是否高于1.15(含),如确实满足要求,则将源代码中的如下部分注释掉。文章来源地址https://www.toymoban.com/news/detail-508455.html
import tensorboard
from setuptools import distutils
# LooseVersion = distutils.version.LooseVersion
# if not hasattr(tensorboard, '__version__') or LooseVersion(tensorboard.__version__) < LooseVersion('1.15'):
# raise ImportError('TensorBoard logging requires TensorBoard version 1.15 or above')
del distutils
#del LooseVersion
del tensorboard
from .writer import FileWriter, SummaryWriter # noqa: F401
from tensorboard.summary.writer.record_writer import RecordWriter # noqa: F401
到了这里,关于【Tensorboard报错解决】AttributeError:module ‘distutils‘ has no attribute ‘version‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!