Unable to use pytorch

amitjc: <p>Dear Experts,</p>
<p>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.</p>
<p>Please guide to resolve this issue.</p>
<p>Thank you.</p>
<p>Best Regards,<br>
Amit.</p>
<pre><code class="lang-auto">(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&gt;=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'
</code></pre>

albanD: <p>Hi,</p>
<p>You need to start python before starting to import libraries.</p>

amitjc: <p>Hello Alban,</p>
<p>I am new to python.</p>
<p>Please elaborate how to start python.</p>
<p>Thank you.</p>
<p>Best Regards,<br>
Amit.</p>

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

amitjc: <p>Hello Alban,</p>
<p>Thanks for the prompt response. That “Works”</p>
<p>How to make it work from Default <code>(base)</code> commandline? Because I need to run TractSeg, which depends on pytorch. PFA TractSeg error log and help.</p>
<p>Thanks.</p>
<p>Best Regards,<br>
Amit.</p>
<pre><code class="lang-auto">(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.
&gt;&gt;&gt; from __future__ import print_function
&gt;&gt;&gt; import torch
&gt;&gt;&gt; x = torch.rand(5, 3)
&gt;&gt;&gt; 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]])
&gt;&gt;&gt; TractSeg
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'TractSeg' is not defined
&gt;&gt;&gt; 
(base) amitjc@AMiT:~$ TractSeg
Traceback (most recent call last):
  File "/home/amitjc/.local/bin/TractSeg", line 20, in &lt;module&gt;
    from tractseg.libs import plot_utils
  File "/home/amitjc/.local/lib/python3.7/site-packages/tractseg/libs/plot_utils.py", line 10, in &lt;module&gt;
    import torch
ModuleNotFoundError: No module named 'torch'
(base) amitjc@AMiT:~$
</code></pre>

albanD: <p>Well pytorch seems properly installed in your main env. As you can see you can import it and use it.<br>
But <code>TrackSeg</code> is a program. So you won’t be able to run that within python.<br>
I don’t know what it is. Can you point to more information about it?</p>
<p>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.</p>

amitjc: <aside class="quote no-group" data-username="albanD" data-post="6" data-topic="76779">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>Can you point to more information about it?</p>
</blockquote>
</aside>
<p><a href="https://github.com/MIC-DKFZ/TractSeg" rel="noopener nofollow ugc">https://github.com/MIC-DKFZ/TractSeg</a></p>

albanD: <p>You want to make sure that you installed this in the same environment as pytorch. Also this uses <code>/usr/bin/env python</code> which might not pick up the conda env <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

amitjc: <p>Thanks much.</p>
<p>Finally I could launch TractSeg in python3 venv after pip Install nightly version of pytorch in python3 venv.</p>
<p>Best Regards,<br>
Amit.</p>