HectorCode
HectorCode

Reputation: 205

Pytorch is installed but is not working on ubuntu 18.04

I am trying to install Pytorch via pip on ubuntu 18.04.I have python 3.6 and my laptop is HP-Pavilion notebook 15

The installation seems to be right because i get the message:

Installing collected packages: torch, torchvision Successfully installed torch-1.3.1+cpu torchvision-0.4.2+cpu

i run the verification code and it is ok

  from __future__ import print_function

  import torch

  x = torch.rand(5, 3)

  print(x)

However, when i close the terminal or reboot and try to run he same code i get the error:

Traceback (most recent call last):

File "torch.py", line 2, in import torch

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

Upvotes: 2

Views: 1831

Answers (3)

Lê Duy Sơn
Lê Duy Sơn

Reputation: 1

Change your file .py to another name, you named torch.py when you import torch it will call ur torch.py

Upvotes: 0

srilekha palepu - Intel
srilekha palepu - Intel

Reputation: 2253

Install pytorch using pip through the below command:

pip3 install torch==1.3.1+cpu torchvision==0.4.2+cpu -f https://download.pytorch.org/whl/torch_stable.html

for any reference go through the official website of pytorch.

Upvotes: 0

JaFizz
JaFizz

Reputation: 356

How are you executing the python script? Which python are you using? Maybe you installed the package in a different python version?

Try to set alias to the python you want to use:

alias python=/usr/local/bin/python3.6

Then pip install the package with that python alias you will always be using.

python pip install <package name>

Python now will install the package in the python files with the alias python - heading to files: /usr/local/bin/python3.6

Let me know if the error still occurs!

Upvotes: 1

Related Questions