Unable to use pytorch

Dear Experts,

I have installed pytorch using conda as well as pip command on ubuntu 19.10 with non-CUDA GPU. However, on attempting verification as metioned on the Get-Started page, Iget error messages which can be found below.

Please guide to resolve this issue.

Thank you.

Best Regards,
Amit.

(base) amitjc@AMiT:~$ pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
Looking in links: https://download.pytorch.org/whl/torch_stable.html
Collecting torch==1.4.0+cpu
  Downloading https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp37-cp37m-linux_x86_64.whl (127.2 MB)
     |████████████████████████████████| 127.2 MB 82 kB/s 
Collecting torchvision==0.5.0+cpu
  Downloading https://download.pytorch.org/whl/cpu/torchvision-0.5.0%2Bcpu-cp37-cp37m-linux_x86_64.whl (5.4 MB)
     |████████████████████████████████| 5.4 MB 38 kB/s 
Requirement already satisfied: numpy in ./.local/lib/python3.7/site-packages (from torchvision==0.5.0+cpu) (1.18.2)
Collecting pillow>=4.1.1
  Downloading Pillow-7.1.1-cp37-cp37m-manylinux1_x86_64.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 8.2 MB/s 
Requirement already satisfied: six in ./miniconda3/lib/python3.7/site-packages (from torchvision==0.5.0+cpu) (1.14.0)
Installing collected packages: torch, pillow, torchvision
Successfully installed pillow-7.1.1 torch-1.4.0+cpu torchvision-0.5.0+cpu
(base) amitjc@AMiT:~$ from __future__ import print_function
from: can't read /var/mail/__future__
(base) amitjc@AMiT:~$ import torch

Command 'import' not found, but can be installed with:

sudo apt install graphicsmagick-imagemagick-compat  # version 1.4+really1.3.33+hg16115-1, or
sudo apt install imagemagick-6.q16                  # version 8:6.9.10.23+dfsg-2.1ubuntu3.1
sudo apt install imagemagick-6.q16hdri              # version 8:6.9.10.23+dfsg-2.1ubuntu3.1

(base) amitjc@AMiT:~$ 
(base) amitjc@AMiT:~$ x = torch.rand(5, 3)
bash: syntax error near unexpected token `('
(base) amitjc@AMiT:~$ print(x)
bash: syntax error near unexpected token `x'

Hi,

You need to start python before starting to import libraries.

Hello Alban,

I am new to python.

Please elaborate how to start python.

Thank you.

Best Regards,
Amit.

Seems like you already have your conda environment loaded (as you can see the (base) on your command line prompt).
So you can just type python in the command line to get into the python REPL where you will be able to start using python command (like imports etc).

Hello Alban,

Thanks for the prompt response. That “Works”

How to make it work from Default (base) commandline? Because I need to run TractSeg, which depends on pytorch. PFA TractSeg error log and help.

Thanks.

Best Regards,
Amit.

(base) amitjc@AMiT:~$ python
Python 3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
tensor([[0.6074, 0.2467, 0.3825],
        [0.1596, 0.0243, 0.4616],
        [0.3431, 0.2790, 0.2505],
        [0.3244, 0.0172, 0.3187],
        [0.1033, 0.8751, 0.3165]])
>>> TractSeg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'TractSeg' is not defined
>>> 
(base) amitjc@AMiT:~$ TractSeg
Traceback (most recent call last):
  File "/home/amitjc/.local/bin/TractSeg", line 20, in <module>
    from tractseg.libs import plot_utils
  File "/home/amitjc/.local/lib/python3.7/site-packages/tractseg/libs/plot_utils.py", line 10, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'
(base) amitjc@AMiT:~$

Well pytorch seems properly installed in your main env. As you can see you can import it and use it.
But TrackSeg is a program. So you won’t be able to run that within python.
I don’t know what it is. Can you point to more information about it?

What is most likely happening is that it has a hardcoded version of python in there that is used instead of the conda one and pytorch is not available in that version of python.

https://github.com/MIC-DKFZ/TractSeg

You want to make sure that you installed this in the same environment as pytorch. Also this uses /usr/bin/env python which might not pick up the conda env :confused:

Thanks much.

Finally I could launch TractSeg in python3 venv after pip Install nightly version of pytorch in python3 venv.

Best Regards,
Amit.