コンテンツに進む

Visual Studio Codeを使ってみる - macOS

旧サイト記事の移行

Get Started Tutorialにそって進めてみる

セクション「Get Started Tutorialにそって進めてみる」

Getting Started with Python

Python 3.0 Release パッケージをインストール

$ which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
$ python3 --version
Python 3.6.5

Visual Studio Code

$ cd Hello
$ python3 -m venv venv

Running VS Code on macOS

$ cat << EOF >> ~/.bash_profile
# Add Visual Studio Code (code)
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF

VSCodeを起動

$ code .

VSCode起動

Configuring Python environments Command + Shift + P Python: Select Interpreter

./venv/bin/python

Python3.6.5(venv) 早速コードを書き始めると linter(pylint)のインストールを促される。 そのままインストール。

Successfully installed astroid-1.6.3 isort-4.3.4 lazy-object-proxy-1.3.1 mccabe-0.6.1 pylint-1.8.4 six-1.11.0 wrapt-1.10.11
You are using pip version 9.0.3, however version 10.0.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

pipのアップデート 最後のメッセージにしたがって、

$ pip install --upgrade pip

ひとまずの環境が整ったようなので動かしてみる。 hello.pyを追加(編集)

print('Hello World!!')

VSCodeのterminalから

$ source venv/bin/activate
$ python hello.py

Unit testing Python in VS Code Command + Shift + P で Command Pallete を開いて Unit と入力。 Command Pallete

Python: Run All Unit Tests

を選択すると、画面右下に次の表示。 Enable and configure a Test Framework 青いボタンをクリックしてテスト環境の設定を進める。 unittest テストコードをtestフォルダにまとめたいので test を選択。 (testフォルダがなければ作成しておく。 test folder testから始まるファイルをテストコードの対象に。 test*.py 出来上がった .vscode/settings.json ではコードを書いていきます。 test/testCalc.py calc.py ステータスバーの [Run Tests] をクリックして [Run All Unit Tests] を実行。 OUTPUT の Python Test Log に OK が表示されればOK。 ただし、テストの2回目以降に、 OK Unhandled exception in thread started by のような例外が発生する模様。 しばらく様子をみます。

VS Code 1.24.0 にバージョンアップしたら上記の例外が出なくなりました。