Azure / MachineLearningNotebooks

Python notebooks with ML and deep learning examples with Azure Machine Learning Python SDK | Microsoft
https://docs.microsoft.com/azure/machine-learning/service/
MIT License
4.07k stars 2.52k forks source link

ModuleNotFound for torch in AML Notebooks, yet Requirement already satisfied? #1875

Closed SeaDude closed 1 year ago

SeaDude commented 1 year ago

I'm working through a Python exercise using Azure Machine Learning notebooks. I'm unable to import torch even after !pip install torch directly in the notebook.

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?

SeaDude commented 1 year ago

Figured it out. Needed to use %pip instead of !pip.