Pythonプロジェクト
Pythonプロジェクトジェネレータは、ベストプラクティスで構成されたモダンなPythonライブラリまたはアプリケーションを作成するために使用できます。これにはUVによる依存関係管理、単一のロックファイルとUVワークスペース内の仮想環境、テスト実行用のpytest、静的解析用のRuffが設定されています。
Pythonプロジェクトの生成
Section titled “Pythonプロジェクトの生成”新しいPythonプロジェクトを2つの方法で生成できます:
- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)
"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - py#project
- 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:py#project
yarn nx g @aws/nx-plugin:py#project
npx nx g @aws/nx-plugin:py#project
bunx nx g @aws/nx-plugin:py#project
パラメータ | 型 | デフォルト | 説明 |
---|---|---|---|
name 必須 | string | - | The name of the Python project |
directory | string | packages | Parent directory where the project is placed. |
projectType 必須 | string | application | Project type |
moduleName | string | - | Python module name |
ジェネレータの出力
Section titled “ジェネレータの出力”ジェネレータは<directory>/<name>
ディレクトリに以下のプロジェクト構造を作成します:
Directory<module-name>
- __init__.py モジュール初期化
- hello.py Pythonソースファイルの例
Directorytests
- __init__.py モジュール初期化
- conftest.py テスト設定
- test_hello.py テストの例
- project.json プロジェクト設定とビルドターゲット
- pyproject.toml UVが使用するパッケージ設定ファイル
- .python-version プロジェクトのPythonバージョン
ワークスペースのルートにも以下のファイルが作成/更新されます:
- pyproject.toml UV用ワークスペースレベルのパッケージ設定
- .python-version ワークスペースのPythonバージョン
- uv.lock Python依存関係のロックファイル
Pythonソースコードの記述
Section titled “Pythonソースコードの記述”Pythonソースコードは<module-name>
ディレクトリに追加します。
他のプロジェクトでのライブラリコードのインポート
Section titled “他のプロジェクトでのライブラリコードのインポート”UVワークスペースが設定されているため、ワークスペース内の他のPythonプロジェクトからプロジェクトを参照できます:
from "my_library.hello" import say_hello
上記の例では、my_library
がモジュール名、hello
はPythonソースファイルhello.py
に対応し、say_hello
はhello.py
で定義されたメソッドです
プロジェクトに依存関係を追加するには、Pythonプロジェクトでadd
ターゲットを実行します:
pnpm nx run my_scope.my_library:add some-pip-package
yarn nx run my_scope.my_library:add some-pip-package
npx nx run my_scope.my_library:add some-pip-package
bunx nx run my_scope.my_library:add some-pip-package
これにより依存関係がプロジェクトのpyproject.toml
ファイルに追加され、ルートのuv.lock
が更新されます。
ランタイムコード
Section titled “ランタイムコード”Pythonプロジェクトをランタイムコード(例: AWS Lambda関数のハンドラ)として使用する場合、ソースコードとすべての依存関係のバンドルを作成する必要があります。project.json
ファイルに以下のようなターゲットを追加することで実現できます:
{ ... "targets": { ... "bundle": { "cache": true, "executor": "nx:run-commands", "outputs": ["{workspaceRoot}/dist/packages/my_library/bundle"], "options": { "commands": [ "uv export --frozen --no-dev --no-editable --project packages/my_library -o dist/packages/my_library/bundle/requirements.txt", "uv pip install -n --no-installer-metadata --no-compile-bytecode --python-platform x86_64-manylinux2014 --python `uv python pin` --target dist/packages/my_library/bundle -r dist/packages/my_library/bundle/requirements.txt" ], "parallel": false }, "dependsOn": ["compile"] }, },}
Pythonプロジェクトにはbuild
ターゲット(project.json
で定義)が設定されており、以下のコマンドで実行できます:
pnpm nx run <project-name>:build
yarn nx run <project-name>:build
npx nx run <project-name>:build
bunx nx run <project-name>:build
<project-name>
はプロジェクトの完全修飾名です。
build
ターゲットはプロジェクトのコンパイル、リント、テストを実行します。
ビルド出力はワークスペースのルートdist
フォルダ内のパッケージとターゲットに対応するディレクトリ(例: dist/packages/<my-library>/build
)に生成されます。
pytestがプロジェクトのテスト用に設定されています。
テストの記述
Section titled “テストの記述”テストはプロジェクト内のtest
ディレクトリにtest_
で始まるPythonファイルとして記述します:
Directorymy_library
- hello.py
Directorytest
- test_hello.py hello.pyのテスト
テストはtest_
で始まるメソッドとして記述し、アサーションを使用して期待値を検証します:
from my_library.hello import say_hello
def test_say_hello(): assert say_hello("Darth Vader") == "Hello, Darth Vader!"
テストの記述方法の詳細についてはpytestドキュメントを参照してください。
テストの実行
Section titled “テストの実行”テストはプロジェクトのbuild
ターゲットの一部として実行されますが、test
ターゲットを個別に実行することもできます:
pnpm nx run <project-name>:test
yarn nx run <project-name>:test
npx nx run <project-name>:test
bunx nx run <project-name>:test
-k
フラグを使用して特定のテストファイルまたはメソッドを指定して実行できます:
pnpm nx run <project-name>:test -k 'test_say_hello'
yarn nx run <project-name>:test -k 'test_say_hello'
npx nx run <project-name>:test -k 'test_say_hello'
bunx nx run <project-name>:test -k 'test_say_hello'
PythonプロジェクトではRUFFを使用してリントを行います。
リントの実行
Section titled “リントの実行”プロジェクトのリントチェックを実行するにはlint
ターゲットを実行します:
pnpm nx run <project-name>:lint
yarn nx run <project-name>:lint
npx nx run <project-name>:lint
bunx nx run <project-name>:lint
リント問題の修正
Section titled “リント問題の修正”ほとんどのリントやフォーマットの問題は自動修正可能です。--configuration=fix
引数を付けて実行することで修正できます:
pnpm nx run <project-name>:lint --configuration=fix
yarn nx run <project-name>:lint --configuration=fix
npx nx run <project-name>:lint --configuration=fix
bunx nx run <project-name>:lint --configuration=fix
ワークスペース内の全パッケージのリント問題を修正するには以下を実行します:
pnpm nx run-many --target lint --all --configuration=fix
yarn nx run-many --target lint --all --configuration=fix
npx nx run-many --target lint --all --configuration=fix
bunx nx run-many --target lint --all --configuration=fix