見出し画像

Windows PowerShell 上の conda 問題

過去の記事で Windows PowerShell と conda の相性が悪さに悩まれされたので、改めて解決策を調査しました。

試行錯誤の果てに、PowerShell 上で conda activate が成功するようになりました。
今回いくつかの対策案を挙げますが、「そのうちのどれが根本対策なのか」までは切り分けていませんので あしからず。

そもそも何が問題か?

Python 言語でプログラミングするときは、仮想環境を使うのが一般的です。
わたしも普段は Miniconda3 という、Anaconda3 の派生版を利用しています。
しかしパッケージ管理ツールの conda が Windows PowerShell 上ではきちんと動作しないのです、コマンドプロンプト上では問題が無いのですが。

手元の PC で PowerShell を開いて、普段使いの Python3.7 仮想環境「dev37」を activate しようとしたのが、以下です。

Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\abech> conda activate dev37

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


PS C:\Users\abech> conda init powershell
no change     C:\Users\abech\Miniconda3\Scripts\conda.exe
no change     C:\Users\abech\Miniconda3\Scripts\conda-env.exe
no change     C:\Users\abech\Miniconda3\Scripts\conda-script.py
no change     C:\Users\abech\Miniconda3\Scripts\conda-env-script.py
no change     C:\Users\abech\Miniconda3\condabin\conda.bat
no change     C:\Users\abech\Miniconda3\Library\bin\conda.bat
no change     C:\Users\abech\Miniconda3\condabin\_conda_activate.bat
no change     C:\Users\abech\Miniconda3\condabin\rename_tmp.bat
no change     C:\Users\abech\Miniconda3\condabin\conda_auto_activate.bat
no change     C:\Users\abech\Miniconda3\condabin\conda_hook.bat
no change     C:\Users\abech\Miniconda3\Scripts\activate.bat
no change     C:\Users\abech\Miniconda3\condabin\activate.bat
no change     C:\Users\abech\Miniconda3\condabin\deactivate.bat
no change     C:\Users\abech\Miniconda3\Scripts\activate
no change     C:\Users\abech\Miniconda3\Scripts\deactivate
no change     C:\Users\abech\Miniconda3\etc\profile.d\conda.sh
no change     C:\Users\abech\Miniconda3\etc\fish\conf.d\conda.fish
no change     C:\Users\abech\Miniconda3\shell\condabin\Conda.psm1
no change     C:\Users\abech\Miniconda3\shell\condabin\conda-hook.ps1
no change     C:\Users\abech\Miniconda3\Lib\site-packages\xontrib\conda.xsh
no change     C:\Users\abech\Miniconda3\etc\profile.d\conda.csh
no change     C:\Users\abech\OneDrive\�h�L�������g\WindowsPowerShell\profile.ps1
No action taken.
PS C:\Users\abech>

まず CommandNotFoundError と言われます。
解決するには "conda init <SHELL_NAME>" を実行せよと言われます。
そこで "conda init powershell" を実行していて、PowerShell を起動し直しました。

しかし状況は変わらず、です。

Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\abech> conda activate dev37

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


PS C:\Users\abech> conad --version
conad : The term 'conad' is not recognized as the name of a cmdlet, function, script fi
le, or operable program. Check the spelling of the name, or if a path was included, ver
ify that the path is correct and try again.
At line:1 char:1
+ conad version
+ ~~~~~
   + CategoryInfo          : ObjectNotFound: (conad:String) [], CommandNotFoundExcept
  ion
   + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\abech> conda --version
conda 4.8.3
PS C:\Users\abech>

conda のバージョンは 4.8.3 で、下記の issue で正式に対応したとされる 4.6 以上なのですが、おかしいですね。

解決策1: Miniconda3(Anaconda3)を再インストール

あなたがお使いの Miniconda3 スタートメニューには、2つの Prompt が表示されているでしょうか?

画像3

もし1つしか表示されていないなら、Miniconda3(Anaconda3)を再インストールした方が良いです。

Note:
わたしが使っていたのは Miniconda3 ver.4.7.10(64bit) で、Prompt は1つだけだったように記憶しています(が、定かではありません)。

Miniconda3 は上書きインストールができないので、以下のように古いバージョンの Miniconda3 をアンインストールしてから、新しいバージョンの Miniconda3 をインストールする必要があります。

手順 1) 仮想環境の退避
・ %USERPROFILE%\miniconda3\envs\ にある仮想環境のうち、残したいものを %USERPROFILE%\Downloads\ あたりへ移動する
※ インストール時のオプション指定によって envs のパスは異なる

手順 2) 古い Miniconda3 のアンインストール
・ 「設定 → アプリケーション → Miniconda3」から「アンインストール」を実行する

手順 3) 新しい Minniconda3 のインストール
・ 新しいバージョンの Miniconda3 インストーラーを起動する
・ ダイアログに従ってオプション等を指定し、インストールを実行する

手順 4) 仮想環境の復元
・ 退避しておいた仮想環境を %USERPROFILE%\miniconda3\envs\ へ移動する

再インストールが終わったら、スタートメニューから「Anaconda3 Powershell Prompt (miniconda3)」を選択することができるはずです。

わたしは Miniconda3 ver.4.8.2(64bit) をインストールしました。

画像3

解決策2: Current User Profile の設定し直し

もしもあなたがすでに起動している PowerShell から conda を使えるようにしたいのなら、この先の対策が必要です。

"conda init powershell" が正しく Current User Profile を編集できているなら、あなたが PowerShell を開いたときに表示されるプロンプトは次のようになるべきです。

(base) PS C:\Users\abech>

先頭に「(base)」があるのが正解です。

そうでないときは、以下のように Current User Profile を編集する必要があります。

手順 1) 誤った Current User Profile を探す
・ "conda init powershell" 実行時の出力内容から、生成された profile.ps1 がドライブのどこにあるかを探し出す

手順 2) 本来の Current User Profile の置き場所の確認
・ PowerShell を開いて $profile の値を表示する
・ このときパスに含まれる文字列「ドキュメント」は、「Documents」に読み替える

手順 3) 本来の Current User Profile の編集
・ 表示されたパスにファイルが無ければ空ファイルを作成し、それをテキストエディターで開く
・ 手順 1 の profile.ps1 の内容一式をコピペして、保存する

わたしの場合、手順 1 の出力はこれ↓でした。

modified      C:\Users\abech\OneDrive\�hL�������g\WindowsPowerShell\profile.ps1

ファイルの実体も同じ場所にありました。
フォルダー名も文字化けしたままです。

画像5

その内容は次のとおりです。

(& "C:\Users\abech\Miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression

手順 2 の出力はこれ↓でした。

C:\Users\abech\OneDrive\ドキュメント\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

このパスの一部を読み替えると、本来の Current User Profile の置き場所になります。

C:\Users\abech\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

手順 3 でファイルを新規作成しました。
その内容は次のとおりです。

Import-Module 'C:\Users\abech\Documents\dev\vcpkg\scripts\posh-vcpkg'
(& "C:\Users\abech\Miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression

なお1行目は別の Profile の残骸(ホームフォルダー付近で見つけたもの↓)からのコピペです。

C:\Users\abech\OneDrive\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

以上の手順で、PowerShell を開くたびに Current User Profile が適用され、conda コマンドが機能するようになりました。

余談: Visual Studio Code の Integrated Terminal

Visual Studio Code では、Integrated Terminal として PowerShell を選択することができます。
Python Extension の公式 GitHub 上で検索すると、PowerShell と conda の組み合わせにまつわる issue が散見されます。

今回紹介した解決策を実施したのち 手元の VS Code May 2020 (version 1.46) で確認したところでは、特に問題なく動作しているように見えます。

画像4

1) VS Code を起動して Ctrl-@ で Integrated Terminal(=PowerShell)を開きます。
すると自動で「base」仮想環境がセットアップされ、プロンプトが表示されました。

画像5

2) 次に、適当に .py ファイルを開きます。
VS Code が Python Language モードになり、Python Interpreter が自動選択されました。
※ ファイルを開いたときに Python Interpreter が未設定である旨のトースト(右下)が表示されることもあります。

ステータス(左下)をクリックすると、conda の各仮想環境を含めたすべての Python Interpreter が列挙されます。

ここで「dev37」仮想環境を選択したあとに Integrated Terminal を開き直すと、今度は「dev37」仮想環境がセットアップされ、プロンプトが表示されました。

画像6

3) さきほど選択した「dev37」仮想環境でのデバッグ実行ももちろん可能です。


この記事が気に入ったらサポートをしてみませんか?