Visual Studio CodeでPythonを始める
このページのタイトル、字幕、要約、読書メモは保存済みの AI 翻訳です。誤りが含まれる場合があります。タイムスタンプは原文と同じです。原文の字幕を開き、元の動画と照合できます。
Tyrique WhiteがVS CodeでPythonをセットアップします。Python拡張機能、インタープリター、仮想環境、TextBlobを使った小さなFlask感情API、そしてデバッグです。
文字起こしの出典:YouTubeに作成者がアップロードしたキャプション。AIによる句読点の付加はVidleafによるもので、元の文言とタイムスタンプのアンカーは保持されています。元の文字起こしは以下でご覧いただけます。
AI生成の要約
Tyrique WhiteがVisual Studio CodeでのPython開発ワークフローを実演します。このチュートリアルでは、エディター、そのPython拡張機能、Pythonインタープリターを区別し、仮想環境がプロジェクトの依存関係をどのように分離するかを説明します。小さなFlask感情APIが、要件のインストール、未解決のインポートの調査、デバッガーを使ったコードのステップ実行のための実行例として使われます。また、保存された起動構成がデバッグセッションの繰り返しにどのように役立つかも示します。メニューラベルやインストールの詳細は、記録された時点のツールのバージョンを反映しています。リンクされた箇所では、環境の選択、依存関係の管理、リクエストのデバッグの背後にある考え方が保持されています。
要点
- [10:32] 仮想環境は、プロジェクトが異なるパッケージ要件間の競合を避けるのに役立ちます。
- [17:56] Flaskの例では、未解決のインポートを、選択された環境とインストール済みの依存関係に関連付けます。
- [23:48] 実行中のAPIにリクエストを送ることで、デバッガーはコードの経路とローカル変数の値を検査できます。
記録から生成されました。リンクされた箇所を元のビデオと照合してください。概要と要点はVidleafが2026-10-21に確認しました。
全体概要
MicrosoftのPython開発者ツール担当プログラムマネージャーであるTyrique Whiteが、Visual Studio CodeでのPython開発の始め方を実演します。VS Codeを無料のオープンソースでクロスプラットフォームなツールであり、拡張機能モデルを持つものと説明し、MicrosoftのPython拡張機能をVS Codeマーケットプレイスで最も人気のある拡張機能として紹介します。これはIntelliSense、デバッグ、テスト、仮想環境とパッケージのサポート、BlackやAutopep8などのフォーマッター、Pylintなどのリンターを提供します。彼はcode.visualstudio.comからのVS Codeのダウンロード、Python拡張機能のインストール、そしてWindowsではpython.orgから、Macではbrew install経由で、Linuxではバンドル版として別途Pythonをインストールする手順を説明し、その後、ファイルエクスプローラー、ソース管理、コマンドパレット、IntelliSense、緑色の実行ボタンについて説明します。パッケージのバージョン競合を避けるために仮想環境がなぜ重要かを説明し、py -3 -m venvで作成する方法を示し、vstoolbox環境を持つSentimentapiという事前に作成されたFlaskアプリを実演します。そこでは、pip install -r requirements.txtを実行するまで、リンティングが未解決のFlaskとtextblobのインポートを指摘します。次に彼は15行目のガターにブレークポイントを設定し、Debug > Start debuggingまたはF5でデバッガーを起動し、Flask構成を選択します。デバッグのセグメントでは、プレゼンターが起動ファイルへのパスを入力し、それをstartup/app.pyに調整します。Enterを押すとツールバーがオレンジ色になり、デバッグアイコンが白くハイライトされてデバッグモードを示します。APIについては、アプリが報告するURLに移動し、「Champion」という単語を送ってその感情を確認することでapp.pyのブレークポイントに到達し、ローカル変数ウィンドウでそれを確認し、F10でステップオーバーし、F5で続行します。その後、APIは「Champion」をポジティブとして表示します。プレゼンターは別の方法でアプリを再実行し、デバッグアイコンのドロップダウンとAdd configurationを使って.vscodeフォルダにlaunch.jsonを作成します。launch.json内でデバッガーを起動するとエラーが発生することに気づき、ボストンから「Wicked cool」をテストするとポジティブが返ることを確認します。このセグメントでは、OSごとに異なるコマンドで環境を作成すること、pip install -r requirements.txt、推奨されるバグアイコンの構成フロー、さらにaka.ms/VST/pythonvscodeのGetting Startedチュートリアル、Flaskのドキュメント、@PythonVSCodeのTwitterハンドルなどのリソースも取り上げます。
タイムスタンプ付きの章の要約
VS CodeでのPython開発
VS CodeのPython拡張機能の概要
- [1:24] このセッションではPythonとVS Codeに焦点を当てます。
- [1:30] VS Codeは無料のオープンソースでクロスプラットフォームのツールであり、すべての言語に対して普及した拡張モデルを採用しています。
- [1:41] このセッションでは、VS CodeのPython拡張機能と入門体験について説明します。
- [1:45] 同僚のJeffreyが後ほど、データサイエンスとPython開発のためのVS Codeについて、この後に録画される別のエピソードで説明します。
- [2:04] Python拡張機能は、IntelliSense、デバッグ、テスト、VMやCondaなどの仮想環境パッケージのサポート、BlackやAutopep8などのフォーマッタ、pylintsなどのリンターといった機能を提供します。
- [2:29] Python拡張機能は、VS Codeマーケットプレイスで最も人気のある拡張機能です。
- [2:38] Visual Studio Codeは現在、Python開発者向けのトップ三ツールの一つです。
- [2:51] 拡張機能であるため、ユーザーはVS Codeをインストールした後に別途インストールする必要があります。
Python用のVS Codeのダウンロードとインストール
- [3:03] Visual Studio Codeはcode.visualstudio.comからダウンロードでき、ユーザーのOSを自動検出します。
- [3:27] VS Codeを開くと、カスタマイズツール付きのウェルカム画面と、インストール済み拡張機能にアクセスするためのアクティビティバーが表示されます。
- [3:43] Pythonを使用するには、拡張機能タブからPythonを検索してPython拡張機能をインストールする必要があります。
- [4:02] 正しい拡張機能は緑の星が付いたMicrosoftのPython拡張機能であり、他のPython拡張機能ではありません。
- [4:38] 第三の要件は、マシンにPythonを別途インストールすることです。VS Code用のPython拡張機能は現在Pythonを同梱していないためです。
- [4:57] Windowsの場合、python.orgから最新のPythonリリースをダウンロードすることをお勧めします。Macではbrew installで問題なく動作します。Linuxでは、バンドルされているバージョンが通常は問題なく動作します。
VS CodeのPythonセットアップと基本
- [5:26] UIは少し異なるため、まずはユーザーに慣れてもらうことから始めます。
- [5:34] ファイルエクスプローラーには、開いたフォルダとその依存関係やファイルのツリー表示が表示されます。
- [5:50] VS Codeには、お気に入りのリポジトリに接続するためのソース管理が標準で付属しています。
- [6:22] デフォルトでは、WindowsにVS CodeをインストールするとVS Codeがパスに追加されます。
- [7:01] コマンドプロンプトにコードを入力すると、そのフォルダ内でVS Codeのインスタンスが開きます。
- [7:15] LinuxとMacでは、VS Codeをパスに追加するためにコマンドを実行する必要があります。
- [7:29] Python拡張機能を有効にするには、Pythonファイル(.py)を選択する必要があります。
- [7:51] ツールバーのPythonをクリックすると、マシンにインストールされているすべてのインタープリタが一覧表示されます。
- [8:15] 拡張機能は、標準的な場所にあるほとんどのインタープリタを検出できます。
- [8:47] Windowsでは、Control Shift Pでコマンドパレットが表示され、PythonコマンドはPythonコロンで示されます。
- [9:21] printと入力するとすぐにIntelliSenseと、パラメータを示すドキュメントツールチップが表示されます。
- [9:47] 数リリース前に追加された緑色の実行ボタンでコードを実行し、ターミナルに「Hello World」と表示します。
VS CodeでのPython仮想環境
- [10:12] 仮想環境はPython開発の重要な概念です。
- [10:26] 二つのアプリケーションが正しく機能するために、それぞれ別のバージョンのパッケージを必要とする場合があります。
- [10:32] 異なるパッケージバージョンを同じ環境に置くと、競合、コードの破損、またはコードが実行されない原因になる可能性があります。
- [10:45] Python開発者は通常、仮想環境を作成することを推奨されています。
- [11:13] このコマンドはpy -3 -m venvの後に選択した名前を使用して、コマンドラインから環境を作成します。
- [11:46] VS Codeはワークスペースフォルダ用に新しい仮想環境を選択するよう促し、そのパスを.vscodeフォルダに追加します。
- [12:25] この環境はワークスペース用のPythonのローカルコピーを作成し、スクリプトやライブラリを含めてそのワークスペースにローカルに配置します。
VS Code Python仮想環境のデモ
- [12:52] スピーカーは仮想環境を実演するために、Sentimentapiという事前に作成されたFlask Webアプリを開きます。
- [13:54] プロジェクトにはvstoolboxという名前の事前に作成された仮想環境があり、再オープン時にも保持されていました。
- [14:11] Windowsでは、コマンドラインからvstoolbox\scripts\activateを実行して仮想環境をアクティブ化でき、環境名が括弧で表示されます。
- [14:45] 仮想環境はソース管理を介してチームメイトや友人と共有できますし、新しい環境を単に作成することもできます。
- [15:03] Windowsでは、control shift back tickで仮想環境が既にアクティブ化された新しいターミナルが開き、activateパスを覚えておく必要がありません。
- [15:31] 黄色の波線はリンティングメッセージです。Python拡張機能はリンターのインストールを求め、現在のデフォルトはPylintで、Flake8などの他のツールもサポートされています。
- [16:01] リンターはFlaskとtextblobの未解決のインポートを報告します。これらのパッケージが仮想環境にないためで、問題パネルで確認できます。
- [16:39] フォルダには、アプリケーションが正しく機能するために必要なすべての依存関係をリストしたrequirements.txtが含まれています。
- [16:52] 好みのソース管理設定に応じて、仮想環境を送信して他の人がダウンロードしてアクティブ化できるようにするか、requirements.txtのみを送信して他の人が環境を作成してパッケージをインストールできるようにします。
- [17:23] アクティブ化された環境内でpip install -r requirements.txtを実行すると、パッケージがローカルにインストールされ、エラーメッセージが消えます。
Flask Sentiment APIアプリの概要
感情分析 API の構築とデバッグ
- [19:01] プレゼンターはデバッガーを使ってアプリケーションを実行し、デバッグについても解説します。
- [19:13] 感情分析 API はシンプルで、コメントと空白を除けばおそらく 10 行未満のコードだと説明されています。
- [19:33] 13 行目には、単語を受け取って感情を返すエンドポイントを作成するルートが含まれています。
- [19:50] このモジュールはメッセージを受け取り、感情スコアがゼロより大きければポジティブ、ゼロより小さければネガティブと判定します。
- [20:01] プレゼンターは textblob パッケージを使用し、入力中は IntelliSense とそのドキュメントに頼っています。
- [20:28] 極性がゼロ未満の場合、感情はネガティブに再割り当てされます。
- [20:50] Textblob には感情分析が組み込まれているため、プレゼンターは車輪の再発明を避けています。
- [21:18] 感情は Web アプリケーションのページに返され、15 行目にブレークポイントが設定されます。
Flask アプリの Python デバッガーの起動
- [21:36] 行番号の左側、ガター内をクリックするとブレークポイントが設定されます。
- [21:41] デバッガーはデバッグに慣れている人には馴染み深く感じられ、そうでない人にとっても始めるのはとても簡単です。
- [21:50] デバッガーはツールバーの「デバッグ」>「デバッグの開始」から、または F5 キーボードショートカットで起動できます。
- [22:10] 「デバッグの開始」をクリックすると、Python 開発者が使用する一般的なテンプレートの一覧を含むデバッグ構成メニューが表示されます。
- [22:24] このアプリは Flask アプリなので、構成リストから Flask を選択します。
- [22:31] Web アプリをデバッグするにはスタートアップファイルへのパスを入力する必要があり、ここでは startup/app.py に調整されています。
- [22:52] Enter キーを押すとデバッグが開始され、デバッグモードを示すためにツールバーがオレンジ色になります。
- [23:04] デバッグ中はデバッグアイコンが白くハイライトされます。
VS Code での Python API のデバッグ
- [23:48] ブレークポイントに到達したら、アプリが実行中と報告する URL に移動します。
- [24:01] VS Code では、その URL をブラウザにコピー&ペーストするか、Ctrl キーを押しながらクリックすることでアクセスできます。
- [24:12] ページには「Python loves VS toolbox」というメッセージが表示されます。
- [24:18] ブレークポイントはメッセージが定義されている app.py に設定されているため、そこに到達するには API にメッセージを送る必要があります。
- [24:37] 彼らは「Champion」という単語を送ってその感情を確認します。
- [24:57] VS Code アイコンがオレンジ色に点滅しているのは、API 内のブレークポイントに到達したことを示しています。
- [25:12] デバッグメニューには、受信したメッセージ「Champion」がローカル変数ウィンドウに表示され、ウォッチウィンドウやコールスタックも並んでいます。
- [25:24] デバッグツールバーまたは F10 でステップオーバーします。すべての感情はまずポジティブに設定され、その後 if 文を通ります。
- [25:44] 矢印または F5 で続行すると、API は「Champion」という単語をポジティブとして表示します。
VS Code のデバッグ構成と感情分析テスト
- [26:11] プレゼンターはデバッガーを使って別の方法でアプリケーションを再実行します。
- [26:21] デバッグアイコンから、ドロップダウンと「構成の追加」を経由して構成メニューにアクセスできます。
- [26:49] 前回の構成が保存されていなかったため、構成を再度入力する必要があります。
- [27:06] Flask に移動すると launched.json というファイルが作成されます。これは構成の JSON 配列です。
- [27:39] 構成は .vscode フォルダに保存され、以前に存在しなかった場合は作成されます。
- [28:11] launch.json 内でデバッガーを起動するとエラーが発生するため、デバッグ対象のファイルに戻る必要があります。
- [28:47] ボストン発の「Wicked cool」をテストすると、Wicked はネガティブで Cool はポジティブですが、Text bot はポジティブだと返します。
- [29:43] ツールバーからデバッグを開始しても構成は保存されません。保存するにはデバッグアイコンと「構成の追加」を使用してください。
Python 環境とデバッグのセットアップ
- [30:10] 新しい環境の作成には、Mac、Linux、Windows のいずれのユーザーかによって異なるコマンドを使用します。
- [30:24] pip install -r requirements.txt コマンドは、プロジェクトにそのようなファイルがあれば、プロジェクトの要件をインストールします。
- [30:31] まず仮想環境を作成し、その後そこに記載されている要件をインストールすることもできます。
- [30:36] 必要に応じて個々のモジュール名をインストールすることもできます。
- [30:44] デバッグには、バグアイコンを使ってデバッグ構成メニューを開き、次にドロップダウン、そして App configuration を選んでデバッグ対象を選択するアプローチが推奨されます。
- [31:04] Web アプリケーションへのパスを入力すると、launch.json ファイルが作成されます。
- [31:09] launch.json ファイルをデバッグすべきではありません。代わりに、対象の Python ファイルに戻って緑色の実行ボタンを押してください。
Python の学習リソースと VS Code
- [31:28] Visual Studio Code での Python の入門チュートリアルは aka.ms/VST/pythonvscode で入手できます。
- [31:46] 入門チュートリアルは他の有用な資料へリンクしており、Flask のドキュメントは Flask を学ぶのに最適だと挙げられています。
- [32:01] ユーザーは Twitter ハンドル @PythonVSCode(すべて一語)を通じてチームと直接やり取りできます。
- [32:11] チームはどの体験が最も効果的でどれがそうでないかを理解するため、常にユーザーとやり取りしています。
- [32:20] ホストはこのセグメントを、Visual Studio Code と Python の始め方への素晴らしい入門だと評しています。
[0:00] >> 今日のVisual Studio Toolboxへようこそ。Tyriqueが、Visual Studio Codeを使ったPython開発の始め方を紹介します。[MUSIC] >> こんにちは、Visual Studio Toolboxへようこそ。ホストのRobert Greenです。今日はTyrique Whiteが参加しています。やあ、Tyrique。>> こんにちはRobert、呼んでくれてありがとう。>> 出演してくれてありがとう。>> ええ、皆さん、Tyrique Whiteです。MicrosoftでPython開発者ツールのプログラムマネージャーをしています。>> いいですね。ではPythonの話をして、Visual Studio Codeを紹介します。
[0:34] >> そうですね。>> それは素晴らしい。では、もう一度非常に簡潔に、以前にも話したと思いますが、Pythonの非常に簡潔なスイートスポットを教えてください。私はCシャープの開発者、またはJavaScriptの開発者ですが、いつ、なぜPythonを学ぶべきですか? 最も一般的なシナリオは何ですか? >> もちろんです。Robert、素晴らしい質問です。会議でよく聞かれます。Pythonのスイートスポットは、アプリを素早く構築できることです。後のデモの一つで、九行のコードで作成できることをお見せします。
[1:07] 非常にシンプルなWebアプリケーションになります。それでもWebアプリケーションを作ります。それがPythonの基盤です。人々が素早くプロトタイプを作れるスクレイピング言語です。しかし本番コードにも使われており、それはとてもわくわくします。>> なるほど。>> ええ、いいですね。>> ええ、素晴らしい。では今日は特にPythonとVS Codeについて話します。ご存じない方のために言うと、VS Codeは無料のオープンソースのクロスプラットフォームツールで、あらゆる言語向けに普及した拡張機能モデルを採用しています。
[1:39] >> ええ。>> では今日は、VS CodeのPython拡張機能と始め方の体験について話します。その後、同僚のJeffreyがデータサイエンスとPython開発のためのVS Codeについて話します。>> それはこの後に録画する別のエピソードになります。>> もちろんです。>> スタジオの舞台裏を見せて、ここでどう動いているかを伝えます。>> ええ。>> そのために同じシャツを着ているんです。>> 素晴らしい。では始めましょう。Python拡張機能は、たくさんの優れた機能を詰め込んで提供しています。
[2:10] それはIntellisense、デバッグ、テスト、VMやCondaのような仮想環境パッケージのサポート、BlackやAutopep8のようないくつかのフォーマッタ、そしてpylintsのようないくつかの人気リンターを提供します。VS CodeのPython拡張機能に関するいくつかの楽しい事実として、それはVS Codeマーケットプレイスで最も人気のある拡張機能です。>> 本当に、Cシャープよりも確実です。>> はい。>> それは興味深い。>> さらに良いことに、Visual Studio Codeは現在、Python開発者向けのトップ三ツールに入っており、それは本当にわくわくします。
[2:44] Pythonには多くの熱狂があり、この講義の終わりまでに皆さんがその理由をもう少し理解してくれることを願っています。以上です。>> つまりそれは拡張機能であり、自分で入手する必要があるということですか?>> はい、その通りです。>> VS Codeをインストールしても、このPython拡張機能は付いてきません。>>そうですね。では、少し話を戻しましょう。Visual Studio Codeをダウンロードしてインストールする方法をお見せしましょう。VS Codeをまだお持ちでない場合、入手するためのリンクはcode.visualstudio.comで、この外部サイトとダウンロードページに来ます。
[3:17] ご覧のとおり、私が使用しているOSを自動検出します。>> なるほど。>> すぐにVS Codeのコピーを入手できます。私はすでにインストールしていますが、要領はお分かりいただけると思います。Visual Studio Codeを入手したら、それを開くと、このウェルカム画面が表示されます。このウェルカム画面にはカスタマイズを可能にするツールがあります。ここには素晴らしいアクティビティバーがあり、インストール済みのすべての拡張機能にアクセスできます。ですから、Visual Studio Codeをダウンロードした後、Pythonを使いたい場合は、Python拡張機能を入手する必要があります。
[3:48] >> なるほど。>> それを行うには、ここの拡張機能タブに移動し、Pythonと入力します。最も人気があるので、一番上に表示され、MicrosoftのPython拡張機能が表示されます。>> Python for VS CodeやPython拡張機能ではないのですか?>> いいえ。ここに緑の星が付いているものが欲しいのです。良い質問ですね。>> あなたが取り組んだものですね。>> 私が取り組んだものです。>> 他のものに問題があるわけではありませんが、これがあなたが取り組んだものなのですか?>> 間違いなく。ご覧のとおり、私はすでにこれをインストールしています。
[4:21] そこで、アンインストールと表示されていますが、それは行いません。ただし、クリックできるインストールバッジが表示されます。また、ここに始め方に関するいくつかのクイックヒントがあり、今日私が取り上げる内容の一部であることがわかります。まず、VS Codeをインストールする必要がありますが、それは済んでいます。拡張機能をインストールする必要がありますが、それも済んでいます。次に三つ目に必要なのは、お使いのマシンにPythonがインストールされていることです。>> そうです。>> ええ。よくある誤解の一つは、VS Code用のPython拡張機能にはPythonが同梱されていると考える人がいることですが、現在は同梱されていません。
[4:52] それを回避する一つの方法は、使用しているOSによって異なります。Windowsの場合、ここにあるpython.orgにアクセスして最新リリースのPythonをダウンロードすることをお勧めします。Macではbrew installで問題なく動作し、Linuxでは通常Linuxに付属するバンドル版のPythonで問題なく動作し、Python拡張機能のこれらすべての機能を利用できます。>> わかりました。>> よさそうですね。>> はい。素晴らしい。>>では、コードを入手し、拡張機能を入手し、Pythonを入手しました。>> そうですね、私はPythonを持っています。
[5:23] 完璧です。では始めましょう。ここでのUIに皆さんを慣れさせましょう。というのも、オンボーディングするのは少し違うからです。>> なるほど。>> では、拡張機能が入ったので。ご覧のとおり、ここにファイルエクスプローラーがあります。フォルダを開くと、そのフォルダのツリービューが表示され、すべての依存関係とその中のすべてのファイルを見ることができます。後で話すデバッガーアイコンがあります。他にもたくさんあります。VS Codeにはソース管理が標準で付属しています。
[5:54] ですから、お気に入りのリポジトリを使って、それらに接続し、慣れ親しんだすべてのことを行うことができます。また、ここにツールバーがあり、見ることができます。私は現在Azureにサインインしています。Live Share拡張機能をインストールしています。ここにツールバーがあり、そういったすべてのものです。>> なるほど。>> では、今からフォルダを作成して、コードをいじり始めましょう。どうですか?>> いいですね。>> もう一つのクールな点は、デフォルトでWindowsにVS Codeをインストールすると、VS Codeがパスに追加されることです。
[6:30] それが何を意味するか正確にお見せします。ですから、私がしたいのはコマンドプロンプトに移動し、ここにあるDemosフォルダを見つけることです。私たちのために新しいフォルダを作成します。VS toolboxと呼びます。そのフォルダに移動します。今、本当にクールなのは、WindowsマシンのパスにVS Codeがあるので、codeと入力するだけでよいことです。
[7:01] コマンドプロンプトに入力すると、そのフォルダ内でVS Codeのインスタンスが開きます。>> なるほど。>> ここにあるファイルエクスプローラーからそれがわかります。>> つまり、codeからフォルダを開くことができ、フォルダに移動してその場所からcodeを起動できるのですか?>> その通りです。LinuxとMacでは、VS Codeをパスに追加するためにコマンドを実行する必要がありますが、コマンドラインを中心に作業する場合には非常に便利です。では、今から実際に新しいファイルを作成します。これがPython拡張機能を有効化する重要な部分です。
[7:33] 拡張機能を有効化するには、下に表示されている「拡張機能を有効化」を確認できますが、Python フォルダを選択する必要があります。失礼、Python ファイルです。>> はい。>> .py がファイルの種類です。ご覧のとおり、すぐに、拡張機能が有効化されたことを確認するために、実際にツールバーに Python が表示されています。これをクリックすると、マシンにインストールされている Python のさまざまなインタープリターの一覧が表示されます。>> Python をインストールしたときは問題ありませんでしたか?
[8:05] >> そうですね、テスト用や試しに使う用など、さまざまな理由で複数の異なるバージョンをインストールしています。しかし、これらが私が持っているすべてのインタープリターのバージョンです。拡張機能は、Python をインストールする際に通常そうである標準的な場所にあれば、それらのほとんどを認識できます。なかなかクールなものです。では、ファイルを作成しました。次に話したいのは、コマンドパレットについてです。
[8:35] これは私たちにとって非常に役立ちます。なぜなら、よりコマンドライン志向の開発者にとっては、エディター内でターミナルの体験が欲しいからです。私がコマンドパレットと呼ぶものを開くには、Windows マシンでは Control Shift P を押してコマンドパレットを表示します。繰り返しますが、コマンドパレットには Python に関するあらゆるコマンドが揃っています。Python のコマンドはすべて「Python:」で示されます。>> はい。>> リスト全体をスクロールして、必要なものを何でも取得できます。私が求めているのは「ターミナルを作成」です。これから話すコマンドライン志向の作業のためにターミナルを開きたいからです。
[9:13] >> はい。>> クールです。ここで本当にクールなのは、編集を開始したり、コードを書き始めたりできることです。print と書くと、すぐにいわゆる IntelliSense とドキュメントが表示され始めるのがわかります。例えば、print ステートメントに詳しくなかったとすると、このドキュメントのツールチップにカーソルを合わせると、どのようなパラメーターを取るのか正確に確認できます。今回は「Hello world」と出力したいだけです。
[9:43] >> はい。>> クールです。これを実行するには、これは実は数リリース前に追加されたものですが、緑色の実行ボタンを使います。このボタンでコードを実行するだけで、ターミナルに「Hello World」と出力されます。>> クールです。>> コーディングの定番の例、「Hello world」です。>> よし。>> 完璧です。クールですね。次に話したいのは、Python 開発にとって本当に重要な仮想環境の概念です。
[10:14] 仮想環境についてご存じですか、Routerさん? >> あまり詳しくはないですね。>> わかりました。では、それが何なのかを簡単に説明します。Pythonでは、アプリケーションAとBを持つことができます。私が開発者で、二つの異なるアプリケーションに取り組んでいるとしましょう。しかし、各アプリケーションが正しく機能するには、それぞれ別のバージョンのパッケージが必要です。>> なるほど。>> もしそれらすべての異なるパッケージバージョンを同じ単一のアプリケーション、つまり同じ環境に含めると、競合が発生する可能性があります。一部のコードが実行されなかったり、何かが壊れたりするかもしれません。
[10:45] そのため、Python 開発者には通常、仮想環境を作成することをお勧めしています。その方法を今からお見せします。クールですね。ではコマンドラインに戻ります。VS toolbox フォルダーにいます。hello.py ファイルがあり、print「Hello world」、すべて問題ありません。使用している OS によってコマンドが異なります。Windows でのコマンドをお見せします。その後、Linux、Mac のコマンドも見てみましょう。コマンドラインから仮想環境を作成して実行するには、Py と入力します。これは Python の略で、-3 は使用する Python のバージョンを指定します。
[11:20] ダッシュ m は、「使用してほしいモジュールを渡します」という意味です。このモジュールは venv と呼ばれ、仮想環境の略で、理にかなっています。仮想環境に名前を付けます。好きな名前を付けてください。好きなフォルダー名は何ですか?>> 好きなフォルダー名は pictures です。>> pictures ですね。クールです。>> 本当にクールなのは、表示されるこのポップアップに気づくことです。そこには「新しい仮想環境が作成されたことに気づきました。ワークスペースフォルダー用にそれを選択しますか?」と表示されます。
[11:51] それが本当にクールなのは、「はい」を選択してここの .vscode フォルダーに移動すると、pictures 仮想環境へのパスを含むこの行が含まれているのがわかります。次にこのフォルダーを開くと、すでにこの仮想環境の中にいて、ここに pictures 仮想環境として示されているのがわかります。そうすれば次回はより早く生産性を高めることができます。>> それはハードドライブ上の実際のフォルダーではないですよね?
[12:22] >> 実際にはローカルコピーを作成します。良い質問ですね。このワークスペース用に Python のローカルコピーを作成し、これに含まれるスクリプトの一覧や、その仮想環境内にある可能性のあるすべてのライブラリを確認できます。しかし繰り返しますが、これはこのワークスペースに対してローカルなだけなので、アプリケーションはその仮想環境内のすべてのものを利用できます。では実際に。>> それは残りますか?コードを終了して戻ってきたときにその環境にアクセスできますか、それとも再作成する必要がありますか?
[12:52] >> その通りです、環境にアクセスできます。なので、今から実際に行うのは、「Hello world」もわくわくしますが、実はすでに仮想環境を作成済みのWebアプリをお見せしたいと思います。>> はい。いいですね。>> では、今すぐあなたの質問に答えましょう。これを閉じるには、File、Close folderに進みます。新しいフォルダを開く方法はいくつかあります。ここでFile、Open Folderに進み、File Explorer上でナビゲートすることもできます。フォルダを開けるスタートメニューもありますが、実際にやりたいデモは「Sentimentapi」で、それは最近使った項目の中にあります。
[13:29] では、なぜそこでクリックしないのでしょうか?いいですね。このアプリケーションについてすぐに気づくことがいくつかあります。これは、シンプルなWebアプリケーションを作成するために広く使われているPythonパッケージであるFlaskを使ったシンプルなWebアプリケーションです。そして、Robertさんの質問に答えると、ここに「vstoolbox」という仮想環境があります。
[13:59] 繰り返しますが、これは事前に作成しておいたので、再度開いたときに保存されていました。さらに良いのは、その仮想環境をターミナル内で実際に二つの方法でアクティブ化できることです。コマンドラインを使う場合、Windowsマシンで仮想環境をアクティブ化する方法は、仮想環境名から始めます。つまり、Windowsなのでvstoolboxバックスラッシュ、scriptsバックスラッシュ、activateと入力すると、仮想環境名が入った括弧によって仮想環境がアクティブ化されたことがわかります。
[14:33] >> では、その情報はソース管理に保存されるのですか?このアプリをGitHubに置き、家に帰ってクローンし、ローカルに持ってくると、コードだけでなく環境情報も得られるのですか?>> ええ、もちろんです。チームの開発者や友人と仮想環境を共有したいなら、間違いなくできます。それもお見せしますし、あるいは新しいものを作ることもできますが、お見せします。それについては少し話しましたが、すぐに全体像が見えてくると思います。この仮想環境をアクティベートするもう一つの簡単な方法として、activateファイルへのパスを覚えておきたくない場合、Windowsマシンではcontrol shift back tickを押すことができます。
[15:12] これにより、この仮想環境がすでにアクティベートされた新しいターミナルが開きます。なかなかクールな機能です。少しエディタを探索してみましょう。皆さん、あるいは全員がおそらく気づいているのは、ここに表示されている黄色い波線です。この黄色い波線は、私たちがlintingメッセージと呼ぶものです。重要なのは、Python拡張機能を初めてインストールするときに、リンターをインストールするかどうかを尋ねられることです。
[15:44] 現在のデフォルトはPylintなので、Pylintを使うかどうかを尋ねられますが、Flake8などの他のlintingツールもサポートしています。ここに表示されているのは、これらのlintingメッセージです。これらのlintingメッセージにホバーすると、「未解決のインポートtext blobが見つかりません」と言っています。つまり、私の仮想環境内でこのパッケージが見つからないということです。
[16:15] エラーが表示されており、これは非常に便利です。コード診断や問題が示され、ここの問題の下でも確認できます。問題のリストがあり、未解決のインポートFlask、未解決のインポートtextblobがあります。そこで実際にできることは、それらをインストールすることです。また、以前作成したフォルダと比べてこのフォルダが異なる点の一つは、ここにrequirements.txtファイルがあることです。ご存じない方のために言うと、Python開発者にとってこれは、アプリケーションが正しく機能するために必要なすべての依存関係のリストにすぎません。
[16:50] では、元の話に戻ります。好みのセットアップやソース管理の種類に応じて、仮想環境をアップロードして人々がダウンロードしてアクティベートできるようにするか、あるいはrequirements.txtファイルだけをアップロードして、人々がまず仮想環境を作成し、次にここにあるすべてのパッケージをインストールできるようにするかもしれません。では、このrequirements.txtファイルからそれらのパッケージをインストールします。コマンドラインに戻ります。繰り返しますが、これらのパッケージをすべてこのアプリケーションにローカルに含めたいので、まだこの仮想環境内にいることに注意してください。
[17:23] では、pip install -r そして requirements.txt と入力して「Enter」を押します。気づくのは、それらのパッケージを見つけてインストールしてくれることです。リストを取得しています。以前にこれらをダウンロードしたことがあるので、これらのキャッシュバージョンを使っているだけです。重要なのは、これらをインストールすると、これらのエラーメッセージが消えることです。これは本当にクールです。
[17:56] パッケージをインストールしている間に、このアプリケーションを少し探索しましょう。先ほど言ったように、これはシンプルなFlaskアプリです。ここでやっているのは、Startupというこのフォルダ内に含まれるFlaskアプリを本質的に作成していることです。ここにシンプルなホームページがあり、「Python loves vstoolbox」とだけ表示されています。これは本当にクールなアプリケーションです。単なるシンプルなWebアプリケーションだけでなく、sentimentapi、つまりフォルダのタイトルが示すものです。
[18:29] これでできるのは、単語を使ってエンドポイントにアクセスできるようになり、このAPIがその単語の感情を判断するのに役立ちます。単語がどのような感情を持つかを判断する助けが必要なだけかもしれませんから。とてもクールです。パッケージのインストールが完了し、問題が消えたことに気づきました。突然Flaskにホバーするとドキュメントが表示されます。Text blobではGet-module text blobが表示され、本当にクールです。では、このアプリケーションを探索してみましょう。
[19:01] では、実際にこのアプリケーションを実行してみましょう。実行する方法はいくつかあります。私はデバッガーを使って実行したいと思います。というのも、デバッグについて、そしてデバッグによって何が得られるのかについて話す機会にもしたいからです。先ほど述べたように、これは感情分析APIで、非常にシンプルで、コメントとスペースをすべて除けばおそらく10行未満のコードです。これもまたPythonの素晴らしさで、先ほどのあなたの指摘の通り、本質的に20行未満のコードで強力な感情分析APIを作成できます。私自身にはいくつかやるべきことがあり、実際にAPIを構築する必要があります。
[19:33] では、13行目までスクロールすると、これが実際に、単語を送信して感情を返してもらえるエンドポイントを作成しているルートです。そこで私がやりたいのは、このモジュールがどのように動作しているかというと、そのメッセージを受け取ると述べており、感情スコアがゼロより大きければ、それをポジティブだと判断します。ゼロ未満ならネガティブです。そこで私がやろうとしているのは、実際にそれを書き出すことです。textblobを使っているので、そのパッケージを使っています。そしてここでIntelliSenseが表示されていることに注目してください。
[20:06] では、タップしてそのメッセージを受け入れます。繰り返しますが、これが私が望んでいるものです。もしメッセージに感情があり、そしてここでもまた、ドキュメント付きのIntelliSenseに注目してください。これにより入力しながら学ぶことができます。polarityがゼロ未満なら、その感情を再割り当てして、「はい、あなたはネガティブです」と言います。同時にタイピングしようとしていて、ここにあるコメントを削除しましょう。
[20:40] >> では、textblobにはどうしてsentimentプロパティがあるのでしょうか?>> 何ですって?>> Textblob.sentimentです。>> ああ、それがtextblobパッケージの素晴らしさです。>> 本当に、感情分析が組み込まれているのですか?>> ええ。>> それはすごい。>> これもPythonの素晴らしい点の一つで、望めばゼロから自分独自の感情分析APIを作ることも十分できますが、なぜ車輪を再発明する必要があるのでしょうか?>> なぜなら、私が使ったことのあるテキストボックスにそれが組み込まれているのを見たことがないからです。>> その通りです。では実際に何をしているかというと、そのメッセージをtextblobモジュールに渡し、内部でいくつかの処理を行い、優れた感情極性などを算出します。
[21:15] >> いいですね。>> では、その感情をネガティブとして再割り当てし、次に実際にやろうとしているのは、その感情をページ上、つまりすぐにご覧いただくWebアプリケーションのページ上に返すことです。では、アプリケーションを実行しましょう。>> わかりました。>> 実はデバッガーを使って実行します。というのも、いくつか話したいことがあるからです。では、15行目にブレークポイントを設定しましょう。行番号の左側、これはガターと呼ばれますが、そこをクリックしたことに注目してください。デバッグに慣れている方には同じような感覚でしょうし、そうでない方も心配いりません。デバッガーを始めるのはとても簡単です。
[21:50] では、これを開始するには、デバッガーを実行する方法がいくつかあります。ここではそれを折りたたんで、皆さんに十分な注意を向けられるようにします。ツールバーを使って「デバッグ」をクリックし、「デバッグの開始」をクリックできます。キーボードショートカットのF5もあります。では、それを始めましょう。「デバッグの開始」をクリックすると、デバッグ構成メニューが表示されます。Python開発者が使うとわかっている人気のテンプレートの一覧があります。
[22:21] 先ほど、これはFlaskアプリだと述べたのを覚えていますか?>> ええ。>> ご覧の通り、Flaskがその構成リストに含まれているので、それが私の望んでいたものであり、それを選択します。Webアプリをデバッグする次のステップは、スタートアップファイルへのパスを入力することです。先ほど、このスタートアップファイルはStartupというフォルダー内に含まれていると述べたのを覚えていますか。ですから実際にやりたいのは、このパスを少し調整して、私のFlaskアプリを作成しているスタートアップファイル、つまりstartup/app.pyを指すようにすることです。
[22:52] Enterを押すとデバッグが開始され、ここにあるツールバーがオレンジ色に変わってデバッグモードであることを示すことに注目してください。もう一つの素晴らしい点は、ここのデバッグアイコンが白くハイライトされることです。ですから、私がデバッグ中であることもわかります。>> やったようですね。>> ええ、やりました。>> わかりました。>> 今まで気づいたことがありませんでした。>> ええ。なかなか粋なことです。>> しかし同時に、覚えていますか、私はこういうことに気づかない傾向があるのですが、Visual Studioでの大きな論争、つまり上部のメニュー選択肢がすべて大文字だったという大きな論争を覚えていますか。
[23:32] >> それは実際、私がMicrosoftにいた時期より前のことです。>> とにかく、論争があったのでそれを発見したのです。>> いや、ええ。>> 私の話はこれくらいにして、アプリに戻りましょう。>> ええ。では、Webアプリをデバッグするには、Webアプリを始めたばかりの人が知らないかもしれないもう一つの点は、15行目に設定したブレークポイントに到達するには、実際にここに表示されているURLに移動する必要があるということです。>> わかりました。>> では、このURLで実行されていると表示されています。そして本当に素晴らしいのは、VS Code内でそのURLにアクセスするには、お好みのブラウザにコピー&ペーストするか、Ctrlキーを押しながらクリックするだけでよいということです。では、そうしてみます。
[24:11] >> おお、見てください。>> 「Python loves VS toolbox」と表示されています。注目してほしいのは、そのメッセージを定義しているapp.py内にこのブレークポイントを設定したので、そのブレークポイントに到達するには、実際にこのAPIにメッセージを送信する必要があるということです。では、やってみましょう。あなたが好きなフレーズ、あるいは感情を知りたいフレーズは何ですか、わかりませんが。>> 「Champion」にしましょう。>> 「Champion」、championの感情がどうなるか見てみましょう。
[24:43] ここでこのホイールが回転していることに注目してください。>> なぜなら、私たちはワールドシリーズの真っ最中で、今日か明日にはチャンピオンが決まるからです。>> おお、すごい。では、センチメントがどうなっているか見てみましょう。VS Codeのここのアイコンがオレンジ色に点滅しているのがわかります。これは何か魔法のようなことをしたという意味です。何をしたかというと、API内のこのブレークポイントにヒットしたのです。デバッグメニューを少し見てみましょう。すると、メッセージ「Champion」を受け取ったことがわかります。
[25:15] これはローカル変数ウィンドウです。ウォッチウィンドウやコールスタックなど、こうした便利なものもすべて使えますし、行をステップ実行することもできます。ステップオーバーするには、そこのデバッグツールバーを使うこともできますし、「F10」を押すこともできます。なので、ここでは[聞き取れず]を使います。ステップを進めると、「okay」と表示されているのがわかります。思い出してください、最初はすべてのセンチメントをポジティブと判定し、その後このif文を通しているのでした。では、もう一度ステップオーバーします。続行するには、ここの矢印を押して最後まで実行することもできますし、「F5」を押すこともできます。
[25:53] では、もう一度「Continue」を押します。APIに戻ってみると、「Champion」という単語がポジティブになっています。>> いいですね。ええ、なかなかクールです。>> では、別のテストをやってみましょう。>> 別のテストをやりましょう。>> はい。新しいメッセージをテストしましょう。>> では、新しいメッセージをテストします。今回はこれを再実行しますが、別の方法で再実行してみます。まだデバッガーを使いますが、少し違う方法です。今回は、15行目にブレークポイントを設定した状態でこのアプリケーションを実行するために、先ほど話したデバッグアイコンを使います。
[26:25] ここで本当にクールなのは、デバッグアイコンから以前見た設定メニューにアクセスできることです。この「ドロップダウン」をクリックして「Add configuration」を選択すると、何が起こるかというと、そして緑色の「play」ボタンを押してデバッグを開始すると、同じデバッグ設定メニューに送られます。>> わかりました。>> 「おいTyrique、なぜこれをまた入力しなければならないんだ?」と思われるかもしれません。それは前回の設定が保存されていなかったからで、今行っているこれらの手順を踏むことでその設定を保存しているのです。それをすぐに証明します。
[26:59] では、再びFlaskに移動してそのパスを入力します。すると、「launched.json」というファイルが作成されるのがわかります。この起動ファイルは、VS Codeを始めたばかりの人にとってはとても怖い、あるいは威圧的に感じられるかもしれませんが、恐れることはありません。これは単なる配列です。JSON形式で、さまざまな設定のリストを持つ配列です。しかし、注目してください。>> すべてが保存されています。
[27:29] >> 保存されますよね、そして起動パスも保存されています。ここにはデフォルトで有効になっているいくつかの引数があります。どこに保存されるかをお見せすると、ここのエクスプローラーボタンを使ってファイルエクスプローラーに戻ると、.VS codeフォルダーがあります。以前に作成されていなければ、自動的に作成され、そのロジック設定が保存されています。これは重要なことです。繰り返しますが、保存するために起動の手順を再度踏む必要がないからです。ではデバッガーに戻りましょう。すると、以前のように「AD configuration」や「no configuration」ではなく、「this time」と表示されているのがわかります。Python flaskが表示されています。
[28:08] デバッグについて理解しておくべきもう一つの重要な点は、私が現在「launch.json」ファイルの中にいることに注目してください。ここでデバッガーを開始すると、このJSONをデバッグしようとしていると判断されて実際にエラーが発生します。それを避けるために、実際にデバッグしようとしているファイルに戻る必要があります。では、緑色の「play」ボタンを押します。もう一度、移動する必要があります。では、別の単語を試してみましょう。
[28:39] >> では、これはセンチメントのテストです。私はボストン出身です。>> わかりました。>> ボストンでは、こういうものが好きなとき、「It's wicked cool」と言います。>> 「Wicked cool」。>> さて、「Wicked」はネガティブなセンチメントですが、「Cool」はポジティブなセンチメントです。では、どうなるか見てみましょう。>> Text botがどう処理するか見てみましょう。では「Wicked cool」。わかりました。またオレンジ色に点滅していることに注目してください。ブレークポイントにヒットしたという意味で、ここの黄色い線でヒットしたことが確認できます。さらに本当にクールなのは、変数ウィンドウで得られるすべての情報は、ここの変数にホバーすることでも実際に確認できることです。
[29:16] >> ええ。>> ツールチップが表示されます。最初はうまく反応しませんでしたが、ほら、最初は恥ずかしがっていましたが、出ました。さまざまなプロパティがすべて見えます。繰り返しますが、続行するには「F5」を押します。これに戻ります。>> 素晴らしい。>> 「Wicked cool is positive」と表示されています。>> いいですね。はい。>> なかなかクールです。>> とても良い。>> はい、とても素晴らしいです。繰り返しになりますが、まとめると、ここのツールバーからデバッグを開始すると、その設定は保存されません。
[29:48] ですので、実際にそれを保存して、次回より生産的になるためには、デバッグアイコンを使ってから構成を追加する必要があります。>> いいですね。>> 本当に本当にすごいものです。では、話したことの少しを振り返りましょう。たくさんのことを扱ったのは分かっていますから。まず、仮想環境の使用について話しましたが、これは異なるアプリケーションのパッケージを管理するのに役立ちます。新しい環境の作成は、MacやLinuxユーザーかWindowsユーザーかによってコマンドがそこにあります。>> はい。>> 次に、私が使ったコマンドで、その時はあまり触れませんでしたが、今から触れるのが、pip install -r requirements.txt です。これは、あなたのファイル、またはプロジェクトにそのようなものがある場合で、まず仮想環境を作成してから、そこにある要件をインストールできます。
[30:36] 必要に応じて個々のモジュール名をインストールすることもできます。>> なるほど。>> デバッグの始め方です。もちろん、これを行う方法は複数ありますが、私が推奨する方法、そして私が好んで行う方法は、バグのアイコンを使ってデバッグ構成メニューを開き、その「ドロップダウン」をクリックして「アプリ構成」を押し、デバッグしたい構成を選択するというものです。>> 二回目は保存していないため、すべてのアクションを再入力する必要があり、ここに直接進むことになります。>> そうです、そのとおりです。そのWebアプリケーションへのパスを入力すると、「launch.json」が作成されます。そして覚えておいてください、「launch.json」ファイルをデバッグするのではなく、実際には関心のあるPythonファイルに戻ってから緑色の実行ボタンを押す必要があります。
[31:17] >> はい。>> なかなかすごいもので、今日皆さんにお伝えするのはだいたいこれくらいです。>> わかりました。>> その始め方の体験について話しているだけです。>> いいですね。では、より詳しい情報については、aka.ms /VST /pythonvscode にアクセスできます。>> はい。>> そこに「Getting Started」チュートリアルがあります。Visual Studio Code内でのPythonに関するより詳しい情報です。Python全般を学ぶには、皆さんにとって良い場所はどこでしょうか?>> オンラインにはたくさんのドキュメントがあり、私たちの「Getting Started」チュートリアルでは、他の役立つ資料へのリンクを教えてくれますが、そうですね、例えばFlaskについて学びたいなら、Flaskのドキュメントは素晴らしいものです。
[32:00] >> はい。>> もし始め方について質問があって、チームと直接やり取りしたい場合は、私たちのTwitterハンドルは at Python VS code です。>> はい。>> すべて一語で、私たちは常にユーザーとやり取りして、どの体験が最もうまく機能し、どれがそうでないかを理解しようとしています。>> 素晴らしい。>> はい。最高です。お招きいただきありがとうございました。>> 来てくれてありがとう。これは、Visual Studio Codeをあまり触ったことがない人々にとってだけでなく、Pythonを使い始める方法についても素晴らしい入門でしたし、最高です。>> 完璧です。はい。
[32:31] >> ありがとう。>> 最高です。ありがとうございました。>> 皆さんが楽しんでくれたことを願っています。次回、Visual Studio Toolboxでお会いしましょう。[音楽]
英語の原文字幕を見る
[0:00] >> On today's Visual Studio toolbox Tyrique is going to show us how to get started with Python development using Visual Studio Code. [MUSIC] >> Hi, welcome to Visual Studio Toolbox. I'm your host Robert Green and joining me today is Tyrique White. Hey Tyrique. >> Hi Robert, thanks for having me. >> Thanks for being on the show. >> Yeah, I'm Tyrique White everyone and I'm a program manager for Python developer tools here at Microsoft. >> Cool. So we're going to talk about doing some Python, we're going to show Visual Studio code.
[0:34] >> Right. >> That's awesome. So give us again the very brief because I think we've talked about it before, but the very brief sweet spot for Python. I is a C-sharp developer, or a JavaScript developer, when and why should I learn Python? What are the scenarios that are most prevalent? >> Definitely. So Robert, excellent question when we get a lot at conferences. The sweet spot for Python is being able to build apps quickly. One of my demos later I'm going to show in nine lines of code I'm able to create.
[1:07] I'll be at a very simple web application. I still create a web application. That's the basis for Python. It's the scrapping language where people are able to prototype quickly. But it's also used for production code as well, which is super exciting. >> Okay. >> Yeah, cool. >> Yeah, awesome. So today specifically we're going to talk about Python and VS Code. For any of those who don't know, VS Code is this free open-source cross-platform tool which uses a popularized extension model for every language.
[1:39] >> Right. >> So today we're going to talk about the Python extension in VS Code and getting started experience. Then later, my colleague Jeffrey will be talking about data science and VS Code for Python development. >> So that will be another episode that we'll record after this one. >> Definitely. >> Showing people behind the scenes and how it works here in the studio. >> Right. >> This way I'm wearing the same shirt. >> Awesome, cool. So let's get started. So the Python Extension offers a lot of great jam-packed features.
[2:10] It offers Intellisense, debugging, testing their support for virtual environment packages like VM and Conda, some formatters like Black and Autopep8, as well as some popular linters like pylints. Some fun facts about Python extension in VS Code, it's the most popular extension than the VS Code marketplace. >> Real, more certain than C-sharp. >> Yes. >> That's interesting. >> Even better is, Visual Studio Code is now at the top three tool for Python developers, which is really exciting.
[2:44] A lot of hype around Python and hopefully by the end of this you guys will understand a bit more why. That's all. >> So it's an extension which implies that you have to go get it? >> Yes, exactly. >> You install VS Code, you don't have this Python extension. >>Right. So yeah. Let's take a step back. Let's show you how to download and install Visual Studio Code. So the link to get Visual Studio Code if you don't have it is code.visualstudio.com and you come to this external and download page.
[3:17] As you can see it auto detects which OS I'm using. >> Okay. >> I can quickly grab a copy of VS Code. I already have it installed but I'm sure we get the idea there. Once you have Visual Studio Code you are going to open it, and you're going to see this welcome screen. This welcome screen has tools that allow you to customize. It has this wonderful activity bar here which gives you access to all of your extensions that you have installed. So after you download Visual Studio Code and you want to use Python, you have to go get the Python extension.
[3:48] >> Okay. >> So to do that you're going to go to the Extensions batch here, and I'm going to type Python. Because it's the most popular, it's going to come up right at the top and you're going to see the Python extension for Microsoft. >> Not the Python for VS Code or the Python extension? >> No. You want the one that has the Green star here. Good question. >> The one that you worked on. >> The one that I worked on. >> Not that there's anything wrong with those other ones, but this is the one you worked on? >> Definitely. As you can see, I've already installed this.
[4:21] So it says uninstall which I'm not going to do, but they'll be an install badge that can be click on. You can also see that we have some quick tips here on how to get started, kind of the things I'm going to cover today. So you need to install VS Code which you've done that. You need to install the extension which I've done. Then the third thing you need is an installation of Python on your machine. >> Right. >> Yeah. One misconception we find is that some people often think that the Python extension for VS Code it comes with Python and it does not currently.
[4:52] So one way to get around that is depending on which OS you're on. For Windows, we recommend going to python.org which I have here to download the latest release of Python, on Mac a brew install works fine and for Linux, typically the bundled version of Python that comes with the Linux works well, you get all of these features of the Python extension. >> Got it. >> Sound good. >> All right. Awesome. >>So now we've got code, we got the extension, we got Python. >> We do, I have python.
[5:23] Perfect. Okay. So let's get started. Let's get people accustomed to the UI here, because it is a bit different to get on-boarded. >> Okay. >> So now that I have the extension. As you can see, I have the File Explorer here. When I open a folder, I'm going to be able to see the tree view of that folder, see all the dependencies and all the files within there. I have a debugger icon which we'll talk about later. I have a lot of other things. VS Code comes inbox with source control.
[5:54] So I can use my favorite repos, connect to those, and do all of that stuff I know and love. We also have the tool bar down here, which allows you to see. I'm signed into Azure currently. I have the life share extension installed. I have my toolbar up here, all that good stuff. >> Okay. >> So what I now want to do is create a folder and let's get started playing around some code. How does that sound? >> Sounds great. >> Another cool thing is, by default when you install VS Code on Windows, it adds VS Code to the path.
[6:30] I'll show you exactly what that means. So what I want to do is I want to navigate to my Command Prompt, and I'm going to find my Demos folder that I have here. I'm going to create a new folder for us. I'm going to call it VS toolbox. I'm going to navigate to that folder. Now what's really cool is, because I have VS Code on the path on my Windows machine, all I have to do is type code.
[7:01] into the Command Prompt, and it will open up an instance of VS Code inside of that folder. >> Okay. >> You can see that from my File Explorer here. >> So you can open the folder from code, you can go to the folder and launch code from that location? >> Exactly. On Linux and Mac you have to run a command to get VS Code on path, but it's super handy if your command line focus. So now what I'm going to do actually is create a new file. This is the important part of activating the Python extension.
[7:33] In order for the extension to activate, which you're seeing down there, activating extension, you need to select a Python folder. Sorry, excuse me, a Python file. >> Okay. >>.Py is the file type. As you can see I'm immediately, to verify that extension is activated, I actually have Python down here in my toolbar. If I click on this, it gives me the list of all the different interpreters I have installed on my machine for Python. >> Is it okay when you installed Python?
[8:05] >> Well, I've installed multiple different versions for different reasons; for testing, for a playing around. But these are all the interpreter versions I have. As the extension can pick up most of those if they are in the standard places which they typically are when you install Python. So pretty cool stuff there. So we created our file. So now what I want to do is I want to talk about the command palette.
[8:35] That's going to be super helpful for us because for developers who are bit more Command Line driven, you want to have that terminal experience inside the editor. So in order to get to what I call the Command Palette, you're going to do on a Windows machine Control Shift P, to bring up the Command Palette. Again, the Command Palette has all of the commands you could ever want and love for Python. All those commands of Python or denoted by Python colon. >> Okay. >> You can scroll down the entire list to get anything that you want. I am after create terminal, because I want to pull up a terminal for some command line driven stuff that I'm going to talk about now.
[9:13] >> Okay. >> Cool. So what's really cool here is that I can start editing or start writing some code. So if I write print, you can see that immediately I'm starting to get what we call intellisense as well as some documentation. So let's say I wasn't familiar with the print statement, I can scroll over to this documentation tool tip here, and I can see exactly what parameters it takes. For this I just want to print "Hello world".
[9:43] >> Okay. >> Cool. To run this, this is actually something we've recently added a couple of releases ago, is the green Run button. So I can actually just run my code with this button and it prints out in the terminal, "Hello World". >> Cool. >> Our classic example for coding,"Hello world". >>All right. >>Perfect. That's cool. Now, the next thing I want to talk about which is really important for Python development is the concept of a virtual environment.
[10:14] Are you familiar with virtual environments router? >> Not that much, no. >> Okay. So I'll explain briefly what those are. So in Python you can have application A and B. Let's say I'm a developer, I'm working on two different applications. However, each application requires separate versions of packages in order to function correctly. >> Okay. >> If I were to include all of those different package versions in the same single application or same environment, there's a possibility that I might have conflicts. Some code may not run, something may get broken.
[10:45] So typically what we recommend for Python developers is to create a virtual environment which I'm going to show you how to do now. Cool. So back to my command line here. So I'm in my VS toolbox folder. I have my hello.Py file, print "Hello world", we're all good. So there are different commands depending on the OS you're using. I'm actually going to show you when we're on those command. Then also we can look at the Linux, Mac command. But in order to run, to create a virtual environment from the command line, what I want to do is I want to type Py, short for Python -3 for the version of Python I'm going to use.
[11:20] Dash m, which is saying, okay, I'm going to pass a module that I want you to use. This module is called VM, short for virtual environment which makes sense. I'm actually going to call our virtual environments. Give me any name you want. What's your favorite folder name? >> My favorite folder name, pictures. >> Pictures. Cool. >> What's really cool is you notice this pop-up that comes. It says. "We noticed a new virtual environment has been created, do you want to select it for the workspace folder?"
[11:51] What's really cool about that is if I select "Yes" and navigate to my.vscode folder here, you'll see that we include this line with a path to that pictures virtual environment. So the next time I open this folder, I'll already be inside of this virtual environment which you can see is being denoted here pictures virtual environment, and so I can get more productive more quickly the next time. >> That's not an actual folder on the hard drive, right?
[12:22] >> So it creates a local copy, actually good question. It creates a local copy of Python for this workspace, and you can see the list of scripts that this includes as well as all of the libraries that you may have in that virtual environment. But again, it's just local to this workspace, so that way your application can get all of the stuff inside of that virtual environment. So I actually. >> It sticks around, if I exit code come back in I can have access to that environment or do I have to recreate it?
[12:52] >> Exactly, you have access to the environment. So what I'm actually going to do now is "Hello world" is exciting, but I actually want to show you a web app that I have with the virtual environment already create. >> Okay. Cool. >> So let's actually answer your question right now. So in order to close this I'm going to go to File, Close folder. There are a couple of ways I can open a new folder. I can go to File, Open Folder here, and navigate on my File Explorer. There's the Start Menu which allows me to open folder, but actually the demo I want is "Sentimentapi" and it's under my recent.
[13:29] So why not just click it there? Cool. So a couple of things to immediately notice about this application, is it's a simple web application using the popular Python package flask which is a weighted just create simple web applications, and to answer your question Robert, I have a virtual environment here called "vstoolbox".
[13:59] Again, I pre-created this so it saved when I opened it back up. What's even better is that I could actually activate that virtual environment inside the terminal in two ways. So if your command line driven again the way in which you're going to activate a virtual environment on a Windows machine is you can start with the virtual environment name, so vstoolbox backslash because we're on Windows, scripts backslash activate and you'll see that the virtual environment is activated by this parentheses with the virtual environment name.
[14:33] >> So then does that information store like in source control. So if I take this app, put it in GitHub, go home, clone it, and bring it down locally I have not only the code but also the environment information? >> Yeah, absolutely. If you want to share the virtual environment with developers in your team or with friends you definitely can do that. I'll also show you or you can just create a new one, but I'll show you. We've talked a little bit about that but I think it'll come full circle in a minute. Another quick way you can activate this virtual environment, if you don't want to have to remember that path to the activate file, what you can do on a Windows machine is control shift back tick.
[15:12] What it'll do is it'll open a new terminal for you with this virtual environment already activated. So some really cool stuff here. Let's explore the editor for a minute. So you guys or everyone has probably noticed the yellow squiggles that are appearing here. These yellow squiggles are what we call linting messages, and so what's important to know is that the first time you install the Python extension it's going to ask if you want to install a linter.
[15:44] The default currently as Pylint, and so it is going to ask you if you want to use Pylint, but we also support other lenting tools like Flake8 etc. So what you're seeing here are these linting messages. So if I hover over those linting messages, it's telling me, it's saying, "Hey, I can't find unresolved import text blob." So what that essentially means, is that, it can't find this package inside of my virtual environments.
[16:15] So it's giving me an error which is super useful because it gives me a code diagnostic or a problem which I can also view under problems here, and you see that I have the list of problems, unresolved import Flask, unresolved import textblob. So what we can do actually is install those. One thing that's also different about this folder compared to the last folder we created, is that, I have this requirements.txt file here, and for people who don't know for Python developers this is just the list of all the dependencies that my application needs to function properly.
[16:50] So back to your original point. Depending on the kind of setup you prefer what source control, you may send up your virtual environments so people can download it and just activate it or you might just send up the requirements.txt file and allow people to first create a virtual environment and then install all the packages thing here. So what I'm going to do is I'm going to install those packages from this requirements.txt file, so back to my command line notice again that I'm still inside this virtual environment because I want all these packages contains locally to this application.
[17:23] So I'm going to go pip install -r and then requirements.txt and hit "Enter". What you're going to notice is you're going to notice that it's going to find those packages and install them for me. It's getting the list, I've downloaded these before so it's just using the cache diversions of these guys. What's important to note is that when it installs these you're going to notice that these error messages are going to go away, which is really cool.
[17:56] So while it's installing those packages let's explore this application a bit. So what this is, as I said before, is the simple Flask gap. So what I'm doing here is I'm essentially creating a Flask gap that's contained inside this folder called Startup. I have a simple homepage here which is just saying, "Python loves vstoolbox," and this is actually a really cool application because it's not only just a simple web application but it's the sentimentapi is the title of the folder denotes.
[18:29] So what this is going to allow us to do, is it's going to allow us to hit an end point with a Word and this API is going to help us determine the sentiment of that word because maybe I just need help determining what kind of sentiment words have. So very cool it finished installing the packages for us, and notice that my problems went away, and suddenly if I hover over Flask I get documentation for it. Text blob I Get-module text blob which is really cool stuff. So now what I want us to do we explore this application.
[19:01] So let's actually run this application. There are couple of ways you can run it. I want to run it using the debugger, because I also want to use this as a moment to talk about debugging and kind of what that gets you. As I stated previously, this is a sentiment API pretty simple only less than perhaps 10 lines of code if you take out all the comments and the spaces, and that's again the beauty of Python's your earlier point in less than 20 lines of code, essentially, I can create a powerful sentiment API. I have some to do for myself actually, I need to actually build the API.
[19:33] So if I scroll down to line 13, this is actually the routes that's creating that endpoint that will allow us to send a word in and get the sentiment back out. So what I want to do is how this module is working is it's saying it's going to take in that message if the sentiment score is greater than zero we're going to determine, we're going to say it's positive. It's less than zero its negative. So what I'm going to do is I'm actually going to write that out. So if textblob I'm using that package and notice I'm getting my IntelliSense here.
[20:06] So I'm going to tap to accept that message. Again, is what I want. So if the message has a sentiment and notice that again this IntelliSense with the documentation which is allowing me to learn as I type.polarity is less than zero, then I'm going to reassign that sentiment, I'm going to say, "Okay your negative." Trying to type at the same time and let's get rid of my comment here.
[20:40] >> So how does text blob have a sentiment property? >> Sorry? >> Textblob.sentiment. >> Oh, it's the beauty of the textblob package. >> Really, it comes with sentiment built-in? >> Yeah. >> That's cool. >> Which is another cool thing about Python, I could very well create my own sentiment API from scratch if I wanted to, but why reinvent the wheel? >> Because I've never seen a text box that I've ever used have that built into it. >> Right. So what it actually does that passes in that message to the textblock module and then it does some stuff under the hood then with cool sentiment polarity etc.
[21:15] >> Cool. >> So we reassign that sentiment has negative, and then what I'm actually going to do is I'm going to return that sentiment on the page, on the web application page which you'll see in a second. So cool. Let's run the application. >> All right. >> I'm going to run it using the debugger actually because there are a couple of things I want to talk about. So let's set a breakpoint on line 15, notice that I clicked to the left of the line number which is called the gutter. For those of you who are used to debugging it's going to be the same kind of feel, for those who aren't fear not it's super easy to get started with the debugger.
[21:50] So to start this, there are a couple of ways I can run my debugger and I'm going to just collapse that so I can give you full attention here. I can use the toolbar, click "Debug" and click "Start debugging." It also has the keyboard shortcut F5 and so what I'm going to do is I'm going to start doing that. Notice that when I click "Start debugging" it's going to present to debug configuration menu. We have a list of popular templates that we know Python developers use.
[22:21] Remember earlier I mentioned that this is a Flask gap? >> Yeah. >> So you can see that Flask is in that configuration list, and so that's what I wanted and I'm going to select it. The next step for debugging a web app is entering the path to my start-up file. Remember I said that this start-up file is contained inside of a folder called Startup, so what I actually want to do is I want to tweak this path a little bit to actually point to my start-up file that is creating my flask gap which is startup/app.py.
[22:52] Once I hit "Enter" it's going to start debugging for me and notice that this toolbar down here changes to orange to denote that I'm in debugging mode. Another cool thing is that debug icon here becomes highlighted in white, so you can see that I'm also debugging as well. >> I guess it did. >> It did, yeah. >> Okay. >> I've never noticed that. >> Yeah. It's a pretty cool nifty thing. >> But at the same time, do you remember, I never tend to notice these things back, you remember the big controversy over in Visual Studio the menu choices at the top are all caps and it was a big controversy.
[23:32] >> That actually predates my time in Microsoft. >> Anyway, I discovered that because there was a controversy. >> No, yeah. >> Enough about me, let's get back to the app. >> Yeah. So to debug a web app, another thing that people getting started with web apps may not know is that in order to hit that break point that I've sent on line 15, I actually need to navigate to the URL that it's giving me here. >> Okay. >> So it tells me that it's running on this URL, and what's really cool is that inside VS code in order to get to that URL, I can either copy and paste it into my browser of choice, or I can just control click, which I'm going to do.
[24:11] >> Oh, look at that. >> You can see it says "Python loves VS toolbox". Notice that because I've set this break point inside of the app.py that defines that message, I actually, in order to hit that break point, I need to send a message to this API. So let's do it. What's the phrase that you love or maybe want to know the sentiment of, I don't know. >> Let's say "Champion". >> "Champion", let's see what the sentiment of champion is.
[24:43] So notice that I have this wheel spinning here. >> Because we are in the middle of the World Series and either today or tomorrow there will be a champion grounds. >> Oh, wow. Well let's see what the sentiment is. So you can tell that VS Code, the icon here is blinking orange, which means that it did something magical. What it's done is it's hit this break point inside of our API. Let's explore the debug menu for a minutes. So notice that it got our message "Champion".
[25:15] This is our locals variable window. I can also have my watch window, I have my call stack, all of this good stuff, and I can also step through lines. So to step over, I could actually use the debug toolbar there or I can just "F10", so I'm just going to use the [inaudible] there. You notice that as I step, it's said "okay" remember we say that we determined all sentiments to be positive at first, and then we run them through this if statement. So I'm just going to step over again. In order to continue, I can either hit the arrow here to continue and run it all the way through, or I can just hit "F5".
[25:53] So I'm going to hit "Continue" again. If I navigate back to my API, I see that the word "Champion" is positive. >> Nice. Yeah, pretty cool. >> All right, let's do another test. >> Let's do another test. >> Yes. Let's test a new message. >> Okay, let's test a new message. This time, I'm going to rerun this, but I'm going to try and rerun it a different way, I'm still using the debugger but a slightly different way. So this time, to run this application with the break point set on line 15, I'm going to use my debug icon that I mentioned earlier.
[26:25] Here's what's really cool about this is that the debug icon gives me access to the configuration menu that I saw before. So if I click this "drop-down" and select "Add configuration", what it's going to do and then I have to hit the green "play" button, start debugging, it's going to send me into that same debug configuration menu. >> Okay. >> You may be wondering, "Oh Tyrique, why do I have to type this again?", and it's because our configuration last time wasn't saved, and so by going through these steps we're going through now, we're saving that configuration and I'll prove that to you in a second.
[26:59] So I'm going to navigate to Flask again, put that path, and notice that it creates this file called "launched.json". We find that this launch file is very scary or intimidating for people who are first starting out with VS code, but fear not, it's simply an array. It's a JSON format, an array with a list of different configurations. But notice that. >> Everything's saved.
[27:29] >> It saves, right, and my startup path is saved. I have some different arguments here which are on by default, and to show you where it gets saved, if you navigate back to your file explorer by using the explorer button here, the dot VS code folder, if there wasn't one created before, it'll be created for you and you'll have that logic configuration saved. This is an important because, again, you don't have to go through the startup steps again to save it. So let's go back to my debugger, and you notice, see it says "this time" instead of "AD configuration" or "no configuration" like it said before, I get Python flask.
[28:08] Another important thing to realize about debugging is that, notice that I'm currently inside of the "launch.json" file. If I start the debugger here it will actually throw an error because it thinks I'm trying to debug this JSON. So in order to avoid that, I need to go back actually to the file that I'm trying to debug. So we're going to hit our green "play" button. Again, I need to navigate to. So let's try another word.
[28:39] >> All right. So this is a test of the sentiment. So I'm from Boston. >> Okay. >> In Boston when we like something like this, we'd say "It's wicked cool". >> "Wicked cool". >> Now "Wicked" has the sentiment of negative but "Cool" has the sentiment of positive. So let's see what happens. >> Let's see how it's Text bot handles it. So "Wicked cool". Okay. Notice again, it's flashing orange, it means it hit the break point, and it's verified that it hits the break point by the yellow line here. What's also really cool is that all of the information you're getting in the variables one here, you can actually hover over variables here.
[29:16] >> Yeah. >> You get the tool tips which it's not playing nice, there we go, it was shy at first, but here we go. You can see all the different properties. Again, to continue, I'm going to press "F5". I'm going to navigate back to this. >> Excellent. >> It says "Wicked cool is positive". >> All right. Yes. >> It's pretty cool. >> Very good. >> Yes, it's very awesome. Again, to recap that, if you start debugging from the toolbar here, you don't get that configuration saved.
[29:48] So in actually to get that saved, to make you more productive than next time, you want to use the debug icon and then an add a configuration. >> Cool. >> Which is really really cool stuff. So let's recap a little bit of what we talked about because I know that we've covered a lot. So using virtual environments, which we talked about to help you manage packages for different applications. So creating new environments, depending on if you're a Mac or Linux user or a Windows user, the commands are there. >> Okay. >> You can then the command I use, that I didn't really touch on at the time but now I'm going to as the pip install - r. requirements.txt, that's if your file or if your project has one of those, you can create a virtual environment first and then you can install the requirements that are there.
[30:36] You can also install individual module names if you want to. >> Okay. >> Getting started with debugging. Of course there are multiple ways you can do this, but the way that I recommend, in the way that I like to do it, is actually using the bug icon which takes me to the debug configuration menu I can click that "Drop-down", hit "App configuration", I can choose my configuration of what I want to debug. >> The second time you have to retype all of the actions because you didn't save it, you're going to hit straight to here. >> Right, exactly. Put the path to that web application, and that "launch.json" gets created, and then remember, you don't want to debug the "launch.json" file, you actually want to go back to the Python file that you are interested in and then hit the green run button.
[31:17] >> Okay. >> Pretty cool stuff and that's about all I have for you guys today. >> All right. >> Just talking about that getting started experience. >> Cool. So for more information, people can go to aka.ms /VST /pythonvscode. >> Yeah. >> There's a Getting Started tutorial there. More information on Python within Visual Studio Code. Where is a good place for people to go to learn Python in general? >> There's a lot of documentation online, our getting started tutorial, we'll teach you links out to other things, other material that is useful, but yes, for example, if you want to learn about Flask, the Flask documentation that they have is excellent.
[32:00] >> Okay. >> If you ever have any questions about getting started though and you want to interact directly with the team, our Twitter handle is at Python VS code. >> Okay. >> All one word, and we are always interacting with users trying to understand what experiences work best and which don't. >> Fantastic. >> Yeah. Awesome. Thanks for having me. >> Thanks for coming on. Those are great introduction to not only Visual Studio code for folks that haven't been playing around with it a lot but also how you get started using Python and it's awesome. >> Perfect. Yes.
[32:31] >> Thanks. >> Awesome. Thank you. >> Hope you guys enjoyed that and we will see you next time on Visual Studio Toolbox. [MUSIC]
Vidleafワークベンチで開く
トランスクリプトを検索し、行を選択し、タイムスタンプ付きの引用をコピーし、翻訳します。
帰属
「Getting Started with Python in Visual Studio Code」by Microsoft Visual Studio(https://www.youtube.com/@visualstudio)、CC BY 3.0(https://creativecommons.org/licenses/by/3.0/)の下でライセンスされています。ソース動画:https://www.youtube.com/watch?v=7EXd4_ttIuw。このページは、段落区切りとタイムスタンプを追加した動画のテキスト文字起こしです。制作者はVidleafと提携しておらず、Vidleafを推奨していません。VidleafはAIによる句読点とAI生成の要約を追加しました。元の文言はこのページで引き続き利用可能です。
あなたは制作者または権利者ですか?訂正や削除をリクエストする:copyright@vidleaf.app(これらのページについてを参照)。
最終更新