LoginSignup
17
11

More than 3 years have passed since last update.

conda activateでエラーが出る話

Posted at

1.はじめに

  • Windows10
  • Anaconda 4.7.12
  • PowerShell

の環境下でconda activateを実行すると下記のエラーログがでる

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 <SHELL_NAME>

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

以下、対処法

方法1.activateにする

PowerShellにこだわりがなければコマンドプロンプトを使う方法
一番堅実

環境変数のPATHに
インストールした場所\Anaconda3
インストールし場所\Anaconda3\Scripts
を加えて再起動

conda activate 環境ではなく
activate 環境にすれば
activate.batから環境に入れる

方法2.conda init <SHELL_NAME>を実行してみる

どうしてもPowerShellが良いんだ!という人向け
または他のshellで方法1が失敗する人

エラーログ通りに
conda init powershell
を実行、今回はpowershellで行う

no change     C:\ProgramData\Anaconda3\Scripts\conda.exe
no change     C:\ProgramData\Anaconda3\Scripts\conda-env.exe
no change     C:\ProgramData\Anaconda3\Scripts\conda-script.py
no change     C:\ProgramData\Anaconda3\Scripts\conda-env-script.py
no change     C:\ProgramData\Anaconda3\condabin\conda.bat
no change     C:\ProgramData\Anaconda3\Library\bin\conda.bat
no change     C:\ProgramData\Anaconda3\condabin\_conda_activate.bat
no change     C:\ProgramData\Anaconda3\condabin\rename_tmp.bat
no change     C:\ProgramData\Anaconda3\condabin\conda_auto_activate.bat
no change     C:\ProgramData\Anaconda3\condabin\conda_hook.bat
no change     C:\ProgramData\Anaconda3\Scripts\activate.bat
no change     C:\ProgramData\Anaconda3\condabin\activate.bat
no change     C:\ProgramData\Anaconda3\condabin\deactivate.bat
modified      C:\ProgramData\Anaconda3\Scripts\activate
modified      C:\ProgramData\Anaconda3\Scripts\deactivate
modified      C:\ProgramData\Anaconda3\etc\profile.d\conda.sh
modified      C:\ProgramData\Anaconda3\etc\fish\conf.d\conda.fish
no change     C:\ProgramData\Anaconda3\shell\condabin\Conda.psm1
modified      C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1
modified      C:\ProgramData\Anaconda3\Lib\site-packages\xontrib\conda.xsh
modified      C:\ProgramData\Anaconda3\etc\profile.d\conda.csh
modified      C:\Users\int\Documents\WindowsPowerShell\profile.ps1

==> For changes to take effect, close and re-open your current shell. <==

上記のログがでるので
shellを再起動し、再びconda activateを実行してみると
またしてもエラー

. : このシステムではスクリプトの実行が無効になっているため、ファイル C:\Users\int\Documents\WindowsPowerShell\profile.p
s1 を読み込むことができません。詳細については、「about_Execution_Policies」(https://go.microsoft.com/fwlink/?LinkID=135
170) を参照してください。
発生場所 行:1 文字:3
+ . 'C:\Users\int\Documents\WindowsPowerShell\profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : セキュリティ エラー: (: ) []、PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

どうやらpermission deniedでスクリプトファイルが読み込めないらしい
ので下記を参考に権限を変えてあげる
http://totech.hateblo.jp/entry/2017/09/29/162411

まず、PowerShellでGet-ExecutionPolicyを実行し
Restrictedになっていることを確認

PS C:\Users\int> Get-ExecutionPolicy
Restricted

直接実行ポリシー設定を変更する

Powershellを管理者として実行し、
Set-ExecutionPolicy RemoteSignedをする

PS C:\Users\int> Set-ExecutionPolicy RemoteSigned

実行ポリシーの変更
実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about_Execution_Policies のヘルプ
トピック (https://go.microsoft.com/fwlink/?LinkID=135170)
で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか?
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)  [?] ヘルプ (既定値は "N"):

YESとした後、再びPowerShellでGet-ExecutionPolicyを実行し
今度はRemoteSignedになっていることを確認

PS C:\Windows\system32> Get-ExecutionPolicy
RemoteSigned

PowerShellを再起動すると

パーソナル プロファイルとシステム プロファイルの読み込みにかかった時間は 643 ミリ秒です。
(base) PS C:\Users\int>

入れます。

問題点

どうやらPowershellを起動する度、base環境に入ってしまうので
C:\Users\<ユーザー名>\Documents\WindowsPowerShell\profile.ps1を

(& "C:\ProgramData\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression;conda deactivate

としてあげると元に戻るのだが
これがセキュリティ的に正しいのか動作的に安定するのか不明なため
デフォルトで使ったほうが安全かと思われる

17
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
11