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?

【0から入門編】 WSL Ubuntu環境でVS CodeからPythonを実行する方法

0
Posted at

WSL Ubuntu環境でVS CodeからPythonを実行する方法

はじめに

WSL (Windows Subsystem for Linux) のUbuntu環境でVS Codeを使ってPythonを実行する際に遭遇したエラーと解決方法をまとめました。

環境

  • OS: Windows (WSL2 + Ubuntu)
  • エディタ: Visual Studio Code
  • 言語: Python 3

発生したエラー

エラー1: VS Codeでフォルダが開けない

Unable to open 'test'
Unable to read file 'vscode-remote://wsl+ubuntu/mnt/c/Users/****/Desktop/test'
(Canceled: Canceled)

エラー2: py コマンドが見つからない

$ py hello.py
Command 'py' not found, but can be installed with:
sudo apt install pythonpy

解決方法

1. VS CodeのWSL拡張機能をインストール

まず、VS CodeでWSL拡張機能をインストールします。

  1. VS Codeの拡張機能タブを開く(Ctrl + Shift + X
  2. 「WSL」で検索
  3. Microsoft製の「WSL」拡張機能をインストール

2. Ubuntuターミナルから正しくVS Codeを起動

cd /mnt/c/Users/****/Desktop/test
code .

VS Codeが起動したら、左下に緑色の「><」アイコンが表示され、「WSL: Ubuntu」と表示されていることを確認します。

3. Pythonの実行コマンドを修正

Ubuntu環境では py コマンドではなく python3 コマンドを使用します。

# ❌ 間違い(Windowsのコマンド)
py hello.py

# ✅ 正解(Ubuntuのコマンド)
python3 hello.py

4. Pythonのインストール確認

もしPythonがインストールされていない場合は、以下のコマンドでインストールします。

# バージョン確認
python3 --version

# インストールされていない場合
sudo apt update
sudo apt install python3 python3-pip

VS Code内でPythonを実行する方法

方法1: 統合ターミナルから実行

  1. VS Codeで Ctrl + @ または Ctrl + Shift + @ でターミナルを開く
  2. 以下のコマンドを実行
python3 hello.py

方法2: Python拡張機能を使用

  1. VS Codeの拡張機能から「Python」(Microsoft製)をインストール
  2. Pythonファイルを開いた状態で右上の▶️(実行ボタン)をクリック
  3. または F5 でデバッグ実行、Ctrl + F5 でデバッグなし実行

便利な設定

エイリアスの設定(オプション)

Windowsと同じように py コマンドを使いたい場合は、エイリアスを設定できます。

# .bashrcに追加
echo "alias py='python3'" >> ~/.bashrc
source ~/.bashrc

これで py hello.py でも実行できるようになります。

VS Codeの推奨設定

プロジェクトフォルダに .vscode/settings.json を作成し、以下を追加すると便利です。

{
    "python.defaultInterpreterPath": "/usr/bin/python3",
    "terminal.integrated.defaultProfile.linux": "bash"
}

サンプルコード

動作確認用のシンプルなPythonスクリプト(hello.py

print("Hello from Ubuntu!")
print("Python test successful!")

# 簡単な計算テスト
x = 10
y = 20
print(f"{x} + {y} = {x + y}")

実行結果:

Hello from Ubuntu!
Python test successful!
10 + 20 = 30

まとめ

  • WSL Ubuntu環境では python3 コマンドを使用する
  • VS CodeのWSL拡張機能をインストールすることで、リモート開発が可能になる
  • Ubuntuターミナルから code . で起動するのが最も確実

参考

  • WindowsとLinuxでPythonコマンドが異なることに注意
    • Windows: py または python
    • Ubuntu/Linux: python3

トラブルシューティング

VS Codeが起動しない場合

# VS CodeのPATHを確認
which code

# PATHが通っていない場合、フルパスで実行
/mnt/c/Users/****/AppData/Local/Programs/Microsoft\ VS\ Code/bin/code .

権限エラーが出る場合

# 権限確認
ls -la /mnt/c/Users/****/Desktop/

# 必要に応じて所有者を変更
sudo chown -R $USER:$USER /mnt/c/Users/****/Desktop/test
0
0
1

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?