AttributeError: module 'torch' has no attribute '__version__'
Asked Answered
M

4

7

I try to check if torch have been properly installed by script:

    import torch
    print(torch.__version__) 

and have the result: AttributeError: module 'torch' has no attribute 'version'

I've checked if torch is installed in my venv by:

    pip freeze

and it is installed(I tried to install as via conda as via pip as suggested at https://pytorch.org/):

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

Environment:

  • OS: Ubuntu 18.04
  • GPU Geforce RTX 3070
  • Driver version 460.39
  • Cuda Toolkit V11.0.194
  • Cudnn V8.0.5.39
  • Python V3.6.9

Do not understand what's the problem

Meath answered 9/2, 2021 at 11:7 Comment(1)
Don't name your file torch.py this will cause your local script to shadow the module named torch. Instead of importing the pytorch module you are importing your local torch.py (/home/evgeniy/cnn/torch.py).Aurthur
K
1

Pytorch has a sub-module called version and from that you can say,
torch.version.__version__

Kennith answered 9/2, 2021 at 11:15 Comment(4)
tried:import torch print(torch.version.__version__) the output is the same: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'Meath
Humph. Note takenKennith
When run from terminal it's okMeath
I have this work form a Jupyter notebook, but not when run directly in Python.Inflammable
D
1

I got the same issue, it was related to a a poorly installed version of torch.

Just pip uninstall torch and pip install torch==2.0.0+cu117 worked for me.

It should also work for another torch version.

Deepseated answered 16/5, 2023 at 14:43 Comment(1)
I also got the same weird error due to a bad installation of torch. The error message did not seem to make sense with the behaviour!Inmost
C
1

I was getting this error in Python jupyter notebooks, trying to run code after just installing packages and Microsoft compilation build tools. I found just restarting the "Python kernel" (python instance running in the background) fixed the problem.

Categorize answered 1/9 at 21:14 Comment(0)
R
0

Comment by @jodag:

Don't name your file torch.py this will cause your local script to shadow the module named torch. Instead of importing the pytorch module you are importing your local torch.py (/home/evgeniy/cnn/torch.py).

Rickie answered 9/2, 2021 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.