属性错误:模块“torch”没有“__version__”属性。

4
我尝试通过脚本检查 torch 是否已经正确安装:
    import torch
    print(torch.__version__) 

并且得到了如下结果:

AttributeError: module 'torch' has no attribute 'version'

我已经检查过我的虚拟环境中是否安装了torch:

    pip freeze

我尝试按照https://pytorch.org/上的建议,通过conda和pip两种方式安装了以下it技术相关的软件包:

  • torch==1.7.1+cu110
  • torchaudio==0.7.2
  • torchvision==0.8.2+cu110

环境信息如下:

  • 操作系统:Ubuntu 18.04
  • GPU型号:Geforce RTX 3070
  • 驱动版本:460.39
  • Cuda Toolkit版本:V11.0.194
  • Cudnn版本:V8.0.5.39
  • Python版本:V3.6.9

不明白问题出在哪里。


4
不要将文件命名为torch.py,这会导致您的本地脚本遮盖了名为torch的模块。而不是导入pytorch模块,您正在导入您的本地torch.py(/home/evgeniy/cnn/torch.py)。 - undefined
3个回答

0
Pytorch有一个名为version的子模块,你可以通过它来获取版本信息,代码如下:
torch.version.__version__

尝试导入torch print(torch.version.version) 输出相同:Traceback (most recent call last): File "torch.py", line 1, in <module> import torch File "/home/evgeniy/cnn/torch.py", line 2, in <module> print(torch.version.version) AttributeError: module 'torch' has no attribute 'version' - undefined
嗯。记下了。 - undefined
在终端中运行时没问题。 - undefined
我有一个Jupyter笔记本的工作表格,但是直接在Python中运行时没有。 - undefined

0

我遇到了同样的问题,这与一个安装不正确的torch版本有关。

只需要运行pip uninstall torchpip install torch==2.0.0+cu117就解决了我的问题。

对于其他版本的torch也应该适用。


0
评论由@jodag:
不要将文件命名为torch.py,这会导致您的本地脚本遮盖名为torch的模块。您导入的是您本地的torch.py(/home/evgeniy/cnn/torch.py),而不是导入pytorch模块。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接