LoginSignup
0
1

More than 1 year has passed since last update.

VSCODEでPythonのインタープリター選択(解決?)

Posted at

目次

  1. 現状
  2. エラー内容
  3. 仮想環境を再作成してみる
  4. settings.jsonの問題?

1. 現状

開発環境

OS : Windows10
Editor : Visual Studio Code

フォルダー構成

D: (ドライブ)
 |--AppData
 |  |--python.exe (Python : 3.10.5)
 |  |--...
 |--MyApp
 |  |--.venv (仮想環境)
 |  |--.vscode
 |  |  |--settings.json
 |  |--flaskr (自作プログラム)
 |  |  |--...

ファイル内容

settings.json
{
   "python.defaultInterpreterPath": ".venv\\Scripts\\python.exe"
}

2. エラー内容

VSCODEでインタープリター選択>デフォルトを指定した際、以下の様なエラーとなる。

無効なPythonインタープリター{0}が選択されています。

3. 仮想環境を再作成してみる

まず、パッケージを逃がしておく。

pip freeze > requirements.txt

.venvを削除し、再度作成する。

# 手動で.venvフォルダーを削除する。
"D:\AppData\python.exe"  -m venv .venv
.venv\scripts\activate.bat
pip install -r requirements.txt

!インタープリター選択と表示されるが、.venvのなかのパッケージは有効になる。
VSCODEを再起動すると、3.10.5('.venv':venv)と表示されるようになった。
インタープリター選択画面にも仮想環境が表示される。
しかし、原因不明で再び仮想環境を認識しない可能性がある。

4. settings.jsonの問題?

こちらの記事によると、書き方が違うような気がする。

参考にして、以下の様に${workspaceFloder}を追加する。

settings.json
{
    "python.defaultInterpreterPath": "${workspaceFolder}\\.venv\\Scripts\\python.exe"
}

こちらに変更して、様子を見てみることにする。

0
1
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
1