個人的なPython練習用リポジトリ
参考資料:"パッケージの操作 - Learn | Microsoft"1
python -m venv env
(envは任意の名前でOK。仮想環境のフォルダ名になる。)
PowerShellのスクリプトファイル(.ps1)を実行するために実行ポリシーの設定2が必要となる。
こちらのサイト(https://www.python.jp/install/windows/venv.html )3を参考に下記コマンドを実行する。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
※-Scope
の設定次第(CurrentUserなど)では一度だけ実行すればOKだけど、セキュリティ面で心配だったのでProcess
にした。
こちらのサイト(https://attakei.net/blog/2019/windows-vscode-venv/ )4を参考に設定。 一度下記設定をすれば、設定したWorkSpaceではVisualStudioCodeを立ち上げるだけで設定が反映される。
- File -> Preference -> Settings -> WorkSpaceタブを選択
- Terminal > Integrated > Env:Windows で Edit in settings.json を選択して、下記のように追加。環境変数としてExecutionPolicyを設定している。
{
"terminal.integrated.env.windows": {
"PSExecutionPolicyPreference": "RemoteSigned"
}
}
.\env\Scripts\Activate.ps1
deactivate
Run and Debugアイコン -> "create a launch.json file." -> "Python File"でlaunch.jsonファイルが作成される。
この状態だと現在VisualStudioCodeで選択されているファイルがデバッグされるが、__main__.py
などに固定したいときは
"configurations"の"program"のところで指定すると変更できる56。
例) "program" : "${workspaceFolder}/src/__main__.py"
(変更したとき時は"name"も変えた方がいいと思う)
Footnotes
-
パッケージの操作 - Learn (Pythonでプロジェクトを作成して管理する) : Microsoft https://docs.microsoft.com/ja-jp/learn/modules/python-create-manage-projects/2-set-up-project ↩
-
実行ポリシーについて - PowerShell | Microsoft Docs : Microsoft https://docs.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2 ↩
-
仮想環境:Python環境構築ガイド(Windows環境のPython) : Python Japan https://www.python.jp/install/windows/venv.html ↩
-
Windows版VisualStudioCodeで、スムーズvenvを使うための設定まとめ: attakei https://attakei.net/blog/2019/windows-vscode-venv/ ↩
-
Debugging in Visual Studio Code (Launch.json attributes) : Visual Studio Code Docs : Microsoft https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes ↩
-
Visual Studio Code Variables Reference : Visual Studio Code Docs : Microsoft https://code.visualstudio.com/docs/editor/variables-reference ↩