To install PyTorch
on Ubuntu, as instructed by the official website, I did pip3 install torch torchvision
, and I am able to run PyTorch
using the python3.5
command.
However, when I run Jupyter Notebook
(I'm just running Jupyter Notebook
in the terminal and using Chrome to access my notebooks), it doesn't recognize the package, throwing ModuleNotFoundError: No module named 'torch'
at me.
The other odd thing is that PyTorch
seems to have only been installed on Python 3.5
and not on Python 3.6
because:
➜ ~ python3.5 -c "import torch; print(torch.__version__)"
0.4.1
➜ ~ python3.6 -c "import torch; print(torch.__version__)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
➜ ~
Therefore I'm guessing that Jupyter Notebook
is not using Python 3.5
.
This is the result when I type python
in my terminal and press TAB
:
➜ ~ python
python python2.7 python3.5 python3.5m python3.6 python3.6m python3-config python3m-config
python2 python3 python3.5-config python3.5m-config python3.6-config python3.6m-config python3m
Also, when I run which python
, I get /home/mhy/anaconda3/bin/python
since I installed Anaconda
recently. I then activated my env
and installed PyTorch
using the following commands:
source activate my_env
conda install torch torchvision
But I wasnt able to run PyTorch
on Anaconda
either.
First, enter anaconda prompt and use the command conda install nb_conda . Second, enter the env of pytorch and use conda install ipykernel . After this, we can find in jupyter notebook, we have more language to use. Choose the language Python [conda env:conda-pytorch] , then we can run code using pytorch successfully.
Make sure you have python 3.7 or higher. To make sure PyTorch is installed in your system, just type python3 in your terminal and run it. After that type import torch for use PyTorch library at last type and run print(torch.
Anaconda is the recommended package manager as it will provide you all of the PyTorch dependencies in one, sandboxed install, including Python and pip.
First, check whether you run your Jupyter notebook on the different environment than the one where PyTorch is installed:
(my_env) instance-1:~$ source activate my_env
(my_env) instance-1:~$ python
>>> import sys
>>> print(sys.executable)
/home/instance-1/anaconda3/envs/my_env/bin/python
and then run the notebook in the my_env
environment:
(my_env) instance-1:~$ jupyter notebook
And in the notebook execute print(sys.executable)
, then if you get something like ‘/home/instance-1/anaconda3/bin/python’
, you have created the notebook in the environment Jupyter uses as a default.
I have been struggling with the same problem, once I discovered that in the Jupyter notebook console you have options to create notebooks in different environments. So just go to the 'New' section and select something like Python(myenv)
to create a notebook in the desired environment(my_env
in this case).
Another possible solution might be to install Jupyter into your environment: activate the desired environment and run conda install -c anaconda jupyter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With