You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

torch._six模块无PY3/PY37属性问题求助(torch1.12.1+cu116)

问题解决:torch._six无PY3/PY37属性

我安装了适配CUDA11.7的PyTorch 1.12.1+cu116版本,但运行代码时发现torch._six模块没有PY3和PY37属性,报错如下:

>>> import torch
>>> torch.__version__
'1.12.1+cu116'
>>> torch._six.PY3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torch._six' has no attribute 'PY3'
>>> torch._six.PY37
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torch._six' has no attribute 'PY37'

以下是几种可行的解决办法:

  • 替换为Python原生版本检测
    torch._six里的PY3/PY37本质是封装的Python版本判断,直接用Python自带的sys.version_info替代即可。把代码中依赖这些属性的部分替换:

    # 替换前
    from torch._six import PY3, PY37
    
    # 替换后
    import sys
    PY3 = sys.version_info >= (3,)
    PY37 = sys.version_info >= (3, 7)
    
  • 降级PyTorch到保留该属性的版本
    PyTorch 1.11.x及更早版本的torch._six模块仍保留PY3/PY37属性,可以安装适配CUDA的旧版本,比如:

    pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113
    

    注:CUDA11.7环境兼容CUDA11.3的PyTorch包。

  • 手动给torch._six添加属性
    不想修改原代码逻辑的话,可以在代码最开头添加以下代码,手动注入这些属性:

    import torch._six
    import sys
    torch._six.PY3 = sys.version_info >= (3,)
    torch._six.PY37 = sys.version_info >= (3, 7)
    

内容的提问来源于stack exchange,提问作者Yu Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 20:55:23