windows环境下使用pathlib库加载pytorch训练好的模型出现不能实例化PosixPath错误。
在pathlib.py文件的1039行做如下修改:
def __new__(cls, *args, **kwargs):
# 源码
# if cls is Path:
# cls = WindowsPath if os.name == 'nt' else PosixPath
# self = cls._from_parts(args, init=False)
# if not self._flavour.is_supported:
# raise NotImplementedError("cannot instantiate %r on your system"
# % (cls.__name__,))
# self._init()
# return self
# 修改
if cls is Path:
# cls = WindowsPath if os.name == 'nt' else PosixPath
cls = WindowsPath
self = cls._from_parts(args, init=False)
# Windows doesn't support PosixPath
if type(self) == PosixPath:
cls = WindowsPath
self = cls._from_parts(args, init=False)
if not self._flavour.is_supported:
raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,))
self._init()
return self
其中上方注释部分为源码,下方的代码为修改代码。