目的
Windows 11上でLeetCodeの問題を手軽に解くための開発環境を構築したので、その手順を共有します。
- あくまで自分用の構成です。汎用性はあまり高くありません。
- Pythonプログラムの実行は、ユニットテスト経由で行います。
手順
ソフトウェアのインストール
以下のソフトウェアをインストールします。
- Visual Studio Code
- Python 3
- Microsoft Storeからインストールしてください。
環境変数の設定
「ユーザー環境変数」に以下を追加します。
- 変数名:PYTHONDONTWRITEBYTECODE
- 値:1
これにより .pyc ファイルが作成されなくなります。
Visual Studio Code の拡張機能インストール
次の拡張機能をインストールします。
-
Python
- インストール時に以下の拡張機能が自動で追加されます。
- Pylance
- Python Debugger
- インストール時に以下の拡張機能が自動で追加されます。
- Black Formatter
Visual Studio Code の拡張機能設定
Ctrl + ,で設定画面を開き、以下の設定を行います。
-
python.analysis.autoImportCompletions- チェックを入れる
-
python.testing.unittestEnabled- チェックを入れる
-
python.analysis.typeCheckingMode-
strictを選択
-
-
python.defaultInterpreterPath-
where python3の実行結果(Python 3 のフルパス)を設定 - 例:
"python.defaultInterpreterPath": "C:\\Users\\hnaka\\AppData\\Local\\Microsoft\\WindowsApps\\python3.exe"
-
言語別設定(Python)
Ctrl + ,で設定画面を開き、以下の言語別設定を追加します。
-
@lang:python editor.formatOnSave- チェックを入れる
-
@lang:python editor.defaultFormatter-
ms-python.black-formatterを選択
-
動作確認
以下の手順で、ユニットテストが正しく動作することを確認します。
1. プロジェクトの作成
まず、新しいPythonプロジェクト用のディレクトリを作成し、Visual Studio Codeで開きます。
mkdir python3_sample
cd python3_sample
code .
2. Pythonファイルの作成
main.py ファイルを作成し、以下のコードを記述します。
class Solution:
def add(self, x: int, y: int) -> int:
return x + y
3. テストファイルの作成
main_test.py ファイルを作成し、以下のようにユニットテストを書きます。
import unittest
from main import Solution
class TestSolution(unittest.TestCase):
def setUp(self) -> None:
self.solution = Solution()
def test_add(self) -> None:
self.assertEqual(self.solution.add(1, 2), 3)
4. テストの実行
- Visual Studio Code 左側のサイドバーにあるフラスコのアイコン(Testing)をクリックします。
- すべてのテストを実行します。
- テスト結果が表示され、すべてが緑色(All Green)になっていることを確認します。
sortedcontainersの導入(2026/02/01追記)
LeetCodeでは外部ライブラリとして「sortedcontainers」の利用が認められています。ここでは、開発環境への導入手順をまとめます。
1. sortedcontainersのインストール
sortedcontainersをインストールします。グローバルインストールではPylanceがうまく認識しない場合があるため、プロジェクト用のフォルダを指定してインストールすることをおすすめします。
pip install sortedcontainers -t <path/to/lib/folder>
例:
pip install sortedcontainers -t C:\python_project\LeetCode\lib
2. Pylanceの設定
Ctrl + ,で設定画面を開き、以下の設定を追加します。
- python.analysis.extraPaths
-
pip install時に指定したフォルダのパスを設定 - 例:
C:\\python_project\\LeetCode\\lib
-
環境情報
- Microsoft Windows [Version 10.0.26100.4652]
- Python 3.13.6
- Visual Studio Code 1.103.0