No module named 'torch' in Jupyter Notebook - jupyter-notebook

I am fairly new to using jupyter notebook, and I've read every forum available for this issue, with no luck.
I am running Windows 11, and I installed Anaconda, then created and activated a virtual environment called pytorchenv. My .yml file includes the following
name: pytorchenv
channels:
defaults
pytorch
dependencies:
numpy=1.16.2
pandas=0.24.2
matplotlib=3.0.3
pillow=5.4.1
pip=19.0
plotly=3.7.0
scikit-learn=0.20.3
seaborn=0.9.0
python=3.7.3
jupyter=1.0.0
pytorch=1.1.0
torchvision=0.2.2
If I list all of the included packages in the command prompt, using
conda list -n pytorch
it shows that pytorch is installed as...
Name Version Build Channel
pytorch 1.1.0 py3.7_cuda100_cudnn7_1 pytorch
What's more, if I enable the virtual environment in the Command Prompt, It seems that I am able to import pytorch successfully
C:\\Users\\Nathaniel\>conda activate pytorchenv
(pytorchenv) C:\\Users\\Nathaniel\>python
Python 3.7.3 (default, Apr 24 2019, 15:29:51) \[MSC v.1915 64 bit (AMD64)\] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import torch
>>>
However, when I try to import pytorch into jupyter notebook, I get the message
import torch
ModuleNotFoundError
Traceback (most recent call last)
Input In \[1\], in \<cell line: 1\>()
1 import torch
ModuleNotFoundError: No module named 'torch'
I have been able to import pandas and numpy in jupyter notebook without issue, but I have not figured out a way to import pytorch successfully.
In case this is helpful, jupyter notebook shows that it is running on
Python 3 (ipykernel)
and running the following in jupyter notebook
import sys
print(sys.executable)
results in
C:\\Users\\Nathaniel\\anaconda3\\python.exe
I've tried checking if there were multiple versions of jupyter notebook on my computer (there aren't).
I also checked that pytorch is installed in the same place as my other anaconda packages (it is).
What am I doing wrong?

The notebook is running with your base environment, as indicated by sys.executable.
Two methods:
Start Jupyter from your environment
Register your environment to be usable by all jupyter notebooks
For that you can find the necessary steps here
Basic step is this
source activate pytorchenv # or activate the environment via conda
python -m ipykernel install --user --name pytorchenv --display-name "Python (pytorchenv)"
Afterwards be sure to restart Juypter, then you should be able to select your pytorchenv kernel.

Related

Azure Machine Learning notebooks: ModuleNotFound error

I'm working through a Python exercise using Azure Machine Learning notebooks. I'm unable to import torch even after !pip install torch.
Notebook says Requirement already satisfied, then errors out with:
!pip install torch
import torch
data = torch.tensor(encode(text), dtype=torch.long)
print(data.shape, data.dtype)
print(data[:100])
4 sec
ModuleNotFoundError: No module named 'torch'
Requirement already satisfied: torch in /anaconda/envs/azureml_py38/lib/python3.8/site-packages (1.12.0)
Requirement already satisfied: typing-extensions in /anaconda/envs/azureml_py38/lib/python3.8/site-packages (from torch) (4.4.0)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Input In [22], in <cell line: 4>()
1 # Encode the entire dataset and store it into a torch.Tensor
3 get_ipython().system('pip install torch')
----> 4 import torch
5 data = torch.tensor(encode(text), dtype=torch.long)
6 print(data.shape, data.dtype)
ModuleNotFoundError: No module named 'torch'
I opened up a terminal in Azure ML Studio and tried pip install torch too, same Requirement already satisfied message showed.
How do I get torch (and any other Python modules where this occurs) working in AML notebooks?
I've found creating environments and installing packages through the terminal to be a much more reliable experience than doing it from an AML notebook.
I suggest using one of the provided terminals (either the one available in the compute instance's details, or the one available in JupyterLab) to create a new conda environment which you can customize to your liking.
Something like:
conda create -n my_tutorial python=3.10
conda activate my_tutorial
pip install --user ipykernel
python -m ipykernel install --user --name=my_tutorial
# Do a complete install of PyTorch, take a look at the available versions here https://pytorch.org/get-started/previous-versions/
conda install pytorch=1.13 torchvision=0.14 torchaudio=0.13 pytorch-cuda=11.7 -c pytorch -c nvidia -y
Afterwards, just make sure your notebooks use the my_tutorial kernel and you should be good to go. Whenever you want to pip install something new, just go back to the terminal, activate your kernel, and install the thing, then it should be available in your notebooks as well.
The issue was lined out in the docs here. Don't use !pip within the notebook. Instead use %pip.

Error when trying to run jupyter notebook: Import error: no module named jupyter_core.command

I am running Ubuntu Linux on my laptop, Anaconda 3 with python version 3.6. Jupyter notebook worked fine for me the last time I used it on this computer, but that was 3 months ago. So today I turn it on and try to run jupyter notebook, and I get this error:
john#john-Satellite-L55Dt-B:~$ jupyter notebook
Traceback (most recent call last):
File "/home/john/anaconda3/bin/jupyter", line 7, in <module>
from jupyter_core.command import main
ImportError: No module named jupyter_core.command
I'll admit I'm completely lost and a little rusty with my LINUX, so what do I need to do to make it work?
Try to export the below path and take jupyter noteboo again.Hope this helps.
export PATH= /home/john/anaconda3/bin:$PATH
You can create a conda environment using the below command:
conda create -n env_sample -c intel python=3.6
To activate the environment use the below command:
source activate env_sample
env_sample is the environment name

"module not found" in jupyter lab, but works fine in "jupyter notebook"

I use conda python environment. I start the Jupyter lab following the steps below:
$conda activate <env_name>
$jupyter lab --no-browser --port=8080 &
Now, from a jupyter lab notebook, when I try to import feather (import feather), it fails with Module Not Found message.
From the jupyter lab notebook, if I execute the following, it shows me that feather is present:
! conda list | grep feather
Now, if I shutdown Jupyter Lab in the same VM and start Jupyter Notebook instead, feather gets imported successfully from the notebook.
$conda activate <env_name>
$jupyter-notebook --no-browser --port=8080 &
I see this discussion, but don't see a solution there.
Alternatively, check your path from within your Jupyter notebook vs on the command line. I found that appending the module paths to sys.path solved this exact issue.
All of the ~/anaconda3/envs/[env]/lib/python3.7* paths were missing in my case.
I have found a possible work around to avoid this issue. This is based on this answer.
From the conda environment (e.g. my_env), I can create a new Python 3 kernel (say, python3_custom). Now, this kernel will be associated with all the libraries installed in that conda environment.
$ conda activate my_env
(my_env)$ conda install ipykernel
(my_env)$ ipython kernel install --user --name=python3_custom
(my_env)$ conda deactivate
I come out of my_env or base environment. Then I start JupyterLab from the command prompt:
jupyter lab --no-browser --port=8080 &
Once I open my notebook now, I can select the kernel as python3_custom. Since in the associated conda environment (my_env), feather is already installed, I don't get the error "Module Not Found" any more.

Do I need to install Jupyter notebook in every virtual environment?

I isolate my data science projects into virtual environments using pipenv. However, running a Jupyter notebok does not access the local environment and uses the default IPyKernel. I've seen that you can register virtual environments from within the environment, but this requires installing the ipykernel package which itself requires Jupyter!
Is there anyway to avoid this and just use a single Jupyter install for all virtual environments?
Generally, you'd install jupyter once and do the following in your virtual environments:
pip install ipykernel
python -m ipykernel install --user
This isn't enough when you're running multiple Python versions.
There's a guide here that tries to address this:
https://medium.com/#henriquebastos/the-definitive-guide-to-setup-my-python-workspace-628d68552e14
It's not 100% failsafe, but it can help you avoid reinstalling jupyter notebook all the time.
I found that there are few problems when reinstall jupyter for each environment separately: i.e. pip install jupyter jupyterlab in new environments.
I had multiple issues (with and without Conda), where Jupyter would install packages to a different python environment when you use !pip install a_package_name within a cell. The shell environment still kept track of the non-environment python, and you can tell this by comparing the outputs of !which python and
import sys
sys.executable
Therefore, when you tried to import the package, it would not be available, because the cells used the environment python/ kernel (as it detected the venv directory).
I found a workaround that I'd appreciate feedback on. I changed pipenv to install virtual environments into the working directory by add to .bashrc/.bash_profile:
export PIPENV_VENV_IN_PROJECT=1
Now when opening a Jupyter notebook, I simply tack on the virtual environment's packages to the Python path:
import sys
sys.path.append('./.venv/lib/python3.7/site-packages/')
Is this a terrible idea?

can't import 'torchtext' module in jupyter notebook while using pytorch

I installed pytorch using anaconda3 and my created virtual conda environment named 'torchTest'.
I installed all the modules needed but, codes doesn't work in jupyter python.
I installed torchtext using
1.pip install https://github.com/pytorch/text/archive/master.zip
2.and also pip install torchtext too.
all I mentioned successfully downloaded in my MAC OS X, but can't get what's wrong with my Jupyter notebook..
After having the same issue with torchtext from within my jupyterlab, I opened an issue on Github at the jupyterlab project as well as at the torchtext repository.
My current solution is to add the PYTHONPATH from the Anaconda env.
The Anaconda path is usually like that $HOME/anaconda/bin
You can add it from within Jupyter Lab/Notebook like that:
import sys
sys.path.append("/some/path/to/add")
import torchtext

Resources