all green

プログラム、アプリ作成、Web等備忘録が中心です

Visual Studio CodeでPythonのプロジェクト作成と実行

前回の記事で作成した環境で実際に動くコードを試してみる

uwanosora22.hatenablog.com


・環境
Windows 10
Python 3.6.3
Visual Studio Code 1.18.1
Python extension for Visual Studio Code 0.8.0

ワークスペースを作成するディレクトリを開く

f:id:uwanosora22:20171215010603j:plain
「フォルダーを開く」をクリック

今回はF:\workspace\pythonとしている

f:id:uwanosora22:20171215010738j:plain
フォルダーの選択


pythonディレクトリが開かれた状態
f:id:uwanosora22:20171215010824j:plain

プロジェクト用ディレクトリを作成する(PowerShellを使用する)

「Ctrl + @」押すとPower Shellの枠が表示される

f:id:uwanosora22:20171215010909j:plain

Power Shellを使用してワークスペースディレクトリ「hello_python」を作成する

mkdir hello_python

f:id:uwanosora22:20171215010954j:plain

作成したディレクトリを開く

code hello_python

f:id:uwanosora22:20171215011533j:plain

f:id:uwanosora22:20171215011553j:plain

Python仮想環境を作成する

hello_pythonディレクトリにPython仮想環境を作成する
「hello_pythonディレクトリが開かれたVisual Studio Code上で「python -m venv venv」

python -m venv venv

f:id:uwanosora22:20171215011732j:plain
「hello_pythonディレクトリの下に「venv」フォルダが作成され仮想環境のファイル一式が格納される

仮想環境の設定

プロジェクト内で仮想環境のPythonを使用するように設定します

コマンドパレットを開く(「Ctrl + Shift + P」を押す)

f:id:uwanosora22:20171215012141j:plain

python select interpreter」と入力する

f:id:uwanosora22:20171215012205j:plain

Python: Select Interpreter」を選択する

f:id:uwanosora22:20171215012326j:plain
使用できるpython.exeのパスが一覧で表示されるので
仮想環境のもの(./venv/Scripts/python.exe)を選択する

ソースコードの作成と実行

「HELLO_PYTHON」の横のボタンからファイルを作成する
f:id:uwanosora22:20171215012356j:plain

ファイル名に「hello_python.py」と入力してEnter
f:id:uwanosora22:20171215012414j:plain

作成するとエラーが表示される

f:id:uwanosora22:20171215012432j:plain

「Install pylint」を選択し、pylintをインストールする

f:id:uwanosora22:20171215012518j:plain
インストール完了

実行するコードを書き、保存する

print('hello python')

f:id:uwanosora22:20171215012719j:plain

実行する

コマンドパレットを開く(「Ctrl + Shift + P」を押す)
python runと入力

f:id:uwanosora22:20171215012806j:plain
表示された一覧の中から「Python: Run Python File in Terminal」を選択する

f:id:uwanosora22:20171215012914j:plain
Power Shell上で実行され結果が表示される