在 conda 环境激活的情况下在 powershell 中使用 Python

问题描述

我有一个名为 condaml 环境,我已使用以下命令在 powershell 中激活此环境:

>> conda init powershell
>> conda activate ml
(ml) >>

当我使用 jupyter notebook 命令打开笔记本时,它工作正常并且我可以访问 ml 环境中的模块:

import torch  # No errors inside a jupyter notebook cell

但是当我尝试在 powershell 和 Python 提示符下测试我的模块时,它不起作用:

(ml) >> py
Python 3.8.6 (tags/v3.8.6:db45529,Sep 23 2020,15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help","copyright","credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
ModuleNotFoundError: No module named 'torch'

我是 conda 环境的新手。有没有办法在 windows powershell 的 Python 提示中使用 conda 环境?

解决方法

使用以下方法确保 pytorch 安装在您的环境中:

conda install -c pytorch pytorch
,

我认为 powershell 中的 conda 初始化一定有问题,请在完成初始化后尝试重新启动 powershell

conda init powershell

然后激活环境然后它应该可以工作