0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonの環境変数ではまった話

Posted at

状況

  1. where pythonを実行したところ、何も表示されなかった

  2. 環境変数には以下の通り登録されていた
    ユーザー環境変数

Path=C:\Users\user-name\AppData\Local\Programs\Python\Python313\Scripts\;
C:\Users\user-name\AppData\Local\Programs\Python\Python313\;
C:\Users\user-name\AppData\Local\Programs\Python\Python310\Scripts\;
C:\Users\user-name\AppData\Local\Programs\Python\Python310\;

システム環境変数

Path=C:\Users\user-name\AppData\Local\Microsoft\WindowsApps\;
C:\Users\user-name\AppData\Local\Programs\Python\Python310\Scripts\;
C:\Users\user-name\AppData\Local\Programs\Python\Python310\;

原因

環境変数は基本的にシステム環境変数よりユーザー環境変数が優先されるが、Pathは異なる
Pathはシステム環境変数の後ろにユーザー環境変数を付け足す(上書きではない)ため、システム環境変数に通したPathが優先される。

そのため、システム環境変数に登録された以下のディレクトリに存在するpythonが最優先されていた。

# Path
C:\Users\user-name\AppData\Local\Microsoft\WindowsApps

# Pathに以下のpythonが存在
C:\Users\user-name\AppData\Local\Microsoft\WindowsApps\python.exe

これはMicrosoft Store経由でインストールされるPythonだが、キャッシュが残っているだけであり実際にはインストールされていなかった。
そのため、where pythonを実行しても何も反応がなかった。

対応

システム環境変数から以下のPathを削除

Path=C:\Users\user-name\AppData\Local\Microsoft\WindowsApps\;
C:\Users\user-name\AppData\Local\Programs\Python\Python310\Scripts\;
C:\Users\user-name\AppData\Local\Programs\Python\Python310\;

結果

where pythonを実行したところ、以下の結果が得られた

C:\Users\user-name\AppData\Local\Programs\Python\Python313\python.exe
C:\Users\user-name\AppData\Local\Programs\Python\Python310\python.exe
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?