问题描述
系统:macOS 10.13.6蟒蛇:3.7蟒蛇3
System: macOS 10.13.6Python: 3.7Anaconda3
在 jupyter notebook 中 import torch
时遇到问题.
I have trouble when import torch
in jupyter notebook.
ModuleNotFoundError: No module named 'torch'
这是我安装pytorch的方法:
Here is how I install pytorch:
conda install pytorch torchvision -c pytorch
我已经检查了 PyTorch 是否安装在我的 anaconda 环境中:
I've checked PyTorch is installed in my anaconda environment:
当我在终端中命令 python3
和 import torch
时,它可以工作.但不适用于 jupyter notebook
When I command python3
in my terminal and import torch
, it works. But not work in jupyter notebook
我试过了:
conda update conda
conda install mkl=2018
但还是一样的错误.
谁能帮我解决这个问题?谢谢!!
Can anyone help me with this? Thanks!!
推荐答案
除了 pytorch
之外,您还必须在激活的 conda 环境中安装 jupyter
.安装步骤如下:
You have to install jupyter
in addition to pytorch
inside your activated conda env. Here is installation steps:
例如:pytorch_p37
和 python 3.7
:
user@pc:~$ conda create -n pytorch_p37 python=3.7
2.激活它
user@pc:~$ conda activate pytorch_p37
或使用(对于较旧的 conda
版本):
Or with (for older conda
versions):
user@pc:~$ source activate pytorch_p37
现在你应该在 shell 提示前看到 (pytorch_p37)
:
Now you should see (pytorch_p37)
before your shell prompt:
(pytorch_p37) user@pc:~$
3.转到 PyTorch 网站 并通过 conda
选择合适的安装命令.例如,在你的 shell 中运行它
3. Go to PyTorch website and choose appropriate installation command via conda
. Run it in your shell, for example
(pytorch_p37) user@pc:~$ conda install pytorch torchvision -c pytorch
4.在激活的环境中也安装 jupyter
(pytorch_p37) user@pc:~$ conda install jupyter
5.验证安装
(pytorch_p37) user@pc:~$ conda list
# packages in environment at /home/user/anaconda3/envs/pytorch_p37:
#
# Name
...
jupyter 1.0.0
jupyter_client 5.3.1
jupyter_console 6.0.0
jupyter_core 4.5.0
...
python 3.7.4
pytorch 1.2.0
...
6.运行 jupyter
(pytorch_p37) user@pc:~$ jupyter notebook
这篇关于无法在 jupyter 笔记本中导入手电筒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!