0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WSL、VSCode、uv でPython開発環境

0
Posted at

uv について

Rust で書かれた、非常に高速な Python パッケージおよびプロジェクト マネージャー

astral-sh/uv: An extremely fast Python package and project manager, written in Rust.

環境

  • Windows 11
  • WSL2(Ubuntu 24.04.1)

VS Code のインストール

公式ページよりインストーラーをダウンロードし、Windows 側にインストールします。

Visual Studio Code 公式ページ

インストール後、VS Code を起動し、画面左の「拡張機能」パネルにて以下の拡張機能をインストールします。

名前 識別子 内容
Python ms-python.python VS Code 用 Python 拡張機能
Ruff charliermarsh.ruff Ruff 拡張機能(Linter & Formatter)

以降は WSL のターミナル上で実施してください。

uv のインストール

WSL を起動し、公式ページの手順に従ってインストールを行います。

uv 公式ページ

curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.local/bin/env
uv --version
# -> インストールした uv のバージョンが表示される

任意のバージョンの Python のインストールを行います。
以下の例では 3.12 3.13 3.14 をインストールしています。

uv python install 3.12 3.13 3.14

プロジェクト作成

uv を使用して、任意のディレクトリでプロジェクト作成を行います。
以下ではuvtestの名前で、Python3.12 を指定して作成しています。

uv init uvtest -p 3.12

プロジェクトのディレクトリを指定して VSCode を起動します。

code uvtest/

VSCode の初期設定

拡張機能のインストール

画面左の「拡張機能」パネルにて、以下の拡張機能をインストールします。

  • Python
  • Ruff

Windows 側の VS Code に上記拡張機能がインストール済みであれば、
一覧上に表示されるので、WSL: Ubuntu・・・ にインストール をクリックすることで、
WSL 側へのインストールが行われます。

VSCode 設定

VSCode の設定に以下を追加します。

{
  "[python]": {
    // デフォルトフォーマッターに Ruff を指定
    "editor.defaultFormatter": "charliermarsh.ruff",
    // 保存、ペースト時にコードフォーマットを実施
    "editor.formatOnSave": true,
    "editor.formatOnPaste": true
  }
}

サンプルプログラムの作成

以降、動作確認のためのサンプルプログラムになります。

Python 仮想環境作成

ctrl + @等でターミナルを開き、以下を実行します。

uv sync

インタープリター選択

main.py を開くと、ウィンドウ右下に Python バージョンが表示されています。

Python_インタープリター設定1.png

作成した仮想環境の Python が選択されていない場合は、クリックして選択してください。

Python_インタープリター設定2.png

実行

main.py を開いた状態で、F5 キーを押下するか
「実行」パネルで「Python: 現在のファイル」を選択して実行ボタンをクリックすることで、
実行されます。

コードフォーマット

VSCode-設定 の設定を行っていれば、
コードの保存時、ペースト時にコードフォーマットが行われます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?