Azure机器学习笔记本:ModuleNotFound错误



我正在使用Azure机器学习笔记本进行Python练习。即使在!pip install torch之后我也无法import torch

笔记本显示Requirement already satisfied,然后错误输入:

!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'

我在Azure ML Studio中打开了一个终端,也尝试了pip install torch,同样的Requirement already satisfied消息显示。

我如何得到torch(和任何其他Python模块,这种情况发生)在AML笔记本工作?

这个问题已经在这里的文档中列出了。不要在笔记本中使用!pip。请使用%pip

我发现通过终端创建环境和安装软件包比在AML笔记本上完成要可靠得多。

我建议使用提供的一个终端(在计算实例的详细信息中可用的一个,或者在JupyterLab中可用的一个)来创建一个新的conda环境,您可以根据自己的喜好进行定制。

类似:

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

之后,只要确保你的笔记本使用my_tutorial内核,你就可以开始了。当你想要pip install一些新的东西时,只要回到终端,激活你的内核,然后安装它,那么它应该也可以在你的笔记本中使用。

最新更新