解决步骤(省流不想看版):
conda init powershell
,初始化condaSet-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
,修改PowerShell限制策略conda activate
是否生效Anaconda已正确安装,但在PowerShell中出现以下问题(CMD可能没事):
(base) PS C:\Users\Administrator>
:如下述例子,用conda activate pytorch
激活pytorch
环境,然后用conda info --envs
显示当前环境,发现仍然为base
,未激活成功;且输入提示的"PS"前不显示(base)
。
PS C:\Users\Administrator> conda info --envs
# conda environments:
#
base * C:\Users\Administrator\anaconda3
pytorch C:\Users\Administrator\anaconda3\envs\pytorch
PS C:\Users\Administrator> conda activate pytorch
PS C:\Users\Administrator> conda info --envs
# conda environments:
#
base * C:\Users\Administrator\anaconda3
pytorch C:\Users\Administrator\anaconda3\envs\pytorch
PS C:\Users\Administrator>
原因:conda在PowerShell中未正确配置,导致conda activate不能用。
其实,存在这种问题时,conda activate是会有报错的,但不知为何,在PowerShell中这个报错没有出现。如果你安装了git,可以在其自带的git bash中输入conda activate your_env
,会出现如下提示:
$ conda activate pytorch
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
To initialize your shell, run
$ conda init
Currently supported shells are:
- bash
- cmd.exe
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
根据上一节的报错提示,我们用conda init
来初始化conda环境:
conda init powershell
该命令的执行结果如下:
PS C:\Users\Administrator> conda init powershell
no change C:\Users\Administrator\anaconda3\Scripts\conda.exe
no change C:\Users\Administrator\anaconda3\Scripts\conda-env.exe
no change C:\Users\Administrator\anaconda3\Scripts\conda-script.py
no change C:\Users\Administrator\anaconda3\Scripts\conda-env-script.py
no change C:\Users\Administrator\anaconda3\condabin\conda.bat
no change C:\Users\Administrator\anaconda3\Library\bin\conda.bat
no change C:\Users\Administrator\anaconda3\condabin\_conda_activate.bat
no change C:\Users\Administrator\anaconda3\condabin\rename_tmp.bat
no change C:\Users\Administrator\anaconda3\condabin\conda_auto_activate.bat
no change C:\Users\Administrator\anaconda3\condabin\conda_hook.bat
no change C:\Users\Administrator\anaconda3\Scripts\activate.bat
no change C:\Users\Administrator\anaconda3\condabin\activate.bat
no change C:\Users\Administrator\anaconda3\condabin\deactivate.bat
no change C:\Users\Administrator\anaconda3\Scripts\activate
no change C:\Users\Administrator\anaconda3\Scripts\deactivate
no change C:\Users\Administrator\anaconda3\etc\profile.d\conda.sh
no change C:\Users\Administrator\anaconda3\etc\fish\conf.d\conda.fish
no change C:\Users\Administrator\anaconda3\shell\condabin\Conda.psm1
no change C:\Users\Administrator\anaconda3\shell\condabin\conda-hook.ps1
no change C:\Users\Administrator\anaconda3\Lib\site-packages\xontrib\conda.xsh
no change C:\Users\Administrator\anaconda3\etc\profile.d\conda.csh
modified C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1
==> For changes to take effect, close and re-open your current shell. <==
PS C:\Users\Administrator>
然后,关闭并重新打开PowerShell,发现出现报红(而且conda acitvate
还是无效):
Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。
安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows
. : 无法加载文件 C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。有关详细信
息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
PS C:\Users\Administrator>
这是由Windows客户端中PowerShell默认为限制(Restricted)的执行策略导致的,接下来将解决这个问题。
进入上述PowerShell报红中给出的网站,可以知道Windows客户端中PowerShell默认的限制策略为Restricted:
下面将直接给出指令来修改该限制策略。关于详细的限制策略信息和修改方法,可以自行参阅报红中给出的网站(https:/go.microsoft.com/fwlink/?LinkID=135170)。
使用以下指令,将限制策略从Restricted改为RemoteSigned,范围为计算机上所有用户:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
再次重启PowerShell,发现conda activate
已可以使用:
Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。
安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows
加载个人及系统配置文件用了 1393 毫秒。
(base) PS C:\Users\Administrator> conda info --envs
# conda environments:
#
base * C:\Users\Administrator\anaconda3
pytorch C:\Users\Administrator\anaconda3\envs\pytorch
(base) PS C:\Users\Administrator> conda activate pytorch
(pytorch) PS C:\Users\Administrator> conda info --envs
# conda environments:
#
base C:\Users\Administrator\anaconda3
pytorch * C:\Users\Administrator\anaconda3\envs\pytorch
(pytorch) PS C:\Users\Administrator>
上述步骤没有成功?
有同学发现,问题也可能是由于在中文系统中开启了OneDrive同步导致的。如果跟着本文的步骤未能解决问题,可以尝试如下博客的解决方法(感谢 IsGrit 同学):
[Windows]解决:无法使用conda activate;IMPORTANT: You may need to close and restart…‘conda init‘无用