Python プロジェクト
Python プロジェクトジェネレーターは、ベストプラクティスで構成された最新の Python ライブラリまたはアプリケーションを作成するために使用できます。UV で管理され、UV ワークスペース内で単一のロックファイルと仮想環境を使用し、テスト実行には pytest、静的解析には Ruff、型チェックには ty を使用します。
Python プロジェクトの生成
Section titled “Python プロジェクトの生成”新しい Python プロジェクトは2つの方法で生成できます:
pnpm nx g @aws/nx-plugin:py#projectyarn nx g @aws/nx-plugin:py#projectnpx nx g @aws/nx-plugin:py#projectbunx nx g @aws/nx-plugin:py#project- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - py#project - 必須パラメータを入力
- クリック
Generate
| パラメータ | 型 | デフォルト | 説明 |
|---|---|---|---|
| name 必須 | string | - | Pythonプロジェクトの名前 |
| directory | string | packages | プロジェクトが配置される親ディレクトリ |
| subDirectory | string | - | プロジェクトが配置されるサブディレクトリ。デフォルトではプロジェクト名になります。 |
| type 必須 | application | library | application | プロジェクトが application か library かを指定します |
| moduleName | string | - | Pythonモジュール名 |
| preferInstallDependencies | boolean | true | ジェネレーター実行後に依存関係のインストールを優先するかどうか。複数のジェネレーターをバッチ処理する際にインストールを延期する場合はfalseに設定します(後続のジェネレーターがNxプロジェクトグラフを計算できるよう、必要に応じてインストールは実行されます)。最後に一度だけインストールします。 |
ジェネレーターの出力
Section titled “ジェネレーターの出力”ジェネレーターは <directory>/<name> ディレクトリに以下のプロジェクト構造を作成します:
Directory<module-name>
- __init__.py Module initialisation
Directorytests
- __init__.py Module initialisation
- conftest.py Test configuration
- test_noop.py Placeholder test
- project.json Project configuration and build targets
- pyproject.toml Packaging configuration file used by UV
- .python-version Contains the project’s Python version
また、ワークスペースのルートに以下のファイルが作成/更新されることに気づくでしょう:
- pyproject.toml Workspace level packaging configuration for UV
- .python-version Contains the workspace Python version
- uv.lock Lockfile for Python dependencies
Python ソースコードの記述
Section titled “Python ソースコードの記述”<module-name> ディレクトリに Python ソースコードを追加します。
他のプロジェクトでライブラリコードをインポートする
Section titled “他のプロジェクトでライブラリコードをインポートする”Python プロジェクトに依存関係を追加するには、add ターゲットを使用します。
2つの Python プロジェクト my_app と my_lib を作成したとします。これらは完全修飾プロジェクト名として my_scope.my_app と my_scope.my_lib を持ち、デフォルトではそれぞれ my_scope_my_app と my_scope_my_lib というモジュール名を持ちます。
my_app が my_lib に依存するようにするには、以下のコマンドを実行します:
pnpm nx run my_scope.my_app:add my_scope.my_libyarn nx run my_scope.my_app:add my_scope.my_libnpx nx run my_scope.my_app:add my_scope.my_libbunx nx run my_scope.my_app:add my_scope.my_libその後、ライブラリコードをインポートできます:
from my_scope_my_lib.my_module import my_function上記で、my_scope_my_lib はライブラリのモジュール名、my_module は Python ソースファイル my_module.py に対応し、my_function はそのファイルで定義されたメソッドです。
プロジェクトに依存関係を追加するには、Python プロジェクトで add ターゲットを実行します。例えば:
pnpm nx run my_scope.my_library:add some-pip-packageyarn nx run my_scope.my_library:add some-pip-packagenpx nx run my_scope.my_library:add some-pip-packagebunx 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 --package my_scope.my_library -o dist/packages/my_library/bundle/requirements.txt", "uv pip install -n --no-deps --no-installer-metadata --no-compile-bytecode --python-platform x86_64-manylinux_2_28 --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 build <project-name>yarn nx build <project-name>npx nx build <project-name>bunx nx build <project-name>ここで <project-name> はプロジェクトの完全修飾名です。
build ターゲットは、プロジェクトのコンパイル、リント、テスト、型チェックを実行します。
ビルド出力は、ワークスペースのルート dist フォルダ内の、パッケージとターゲット用のディレクトリ(例:dist/packages/<my-library>/build)にあります。
ワークスペース内のすべてのプロジェクトをビルドするには、以下を実行します:
pnpm nx run-many --target buildyarn nx run-many --target buildnpx nx run-many --target buildbunx nx run-many --target buildまたは短縮コマンドを使用します:
pnpm buildyarn buildnpm run buildbun buildプロジェクトのテストには pytest が設定されています。
テストの記述
Section titled “テストの記述”テストは、プロジェクト内の test ディレクトリに、test_ で始まる Python ファイルに記述する必要があります。例えば:
Directorymy_library
- my_module.py
Directorytests
- test_my_module.py Tests for my_module.py
テストは test_ で始まるメソッドで、期待値を検証するためのアサーションを行います。例えば:
from my_library.my_module import say_hello
def test_say_hello(): assert say_hello("Darth Vader") == "Hello, Darth Vader!"テストの記述方法の詳細については、pytest ドキュメントを参照してください。
テストの実行
Section titled “テストの実行”テストはプロジェクトの build ターゲットの一部として実行されますが、test ターゲットを実行することで個別に実行することもできます:
pnpm nx test <project-name>yarn nx test <project-name>npx nx test <project-name>bunx nx test <project-name>-k フラグを使用して、テストファイルまたはメソッドの名前を指定することで、個別のテストまたはテストスイートを実行できます:
pnpm nx test <project-name> -k 'test_say_hello'yarn nx test <project-name> -k 'test_say_hello'npx nx test <project-name> -k 'test_say_hello'bunx nx test <project-name> -k 'test_say_hello'Python プロジェクトは型チェックに ty を使用します。
型チェッカーの実行
Section titled “型チェッカーの実行”型チェックはプロジェクトの build ターゲットの一部として実行されますが、typecheck ターゲットを介して個別に実行することもできます:
pnpm nx run <project-name>:typecheckyarn nx run <project-name>:typechecknpx nx run <project-name>:typecheckbunx nx run <project-name>:typecheck型エラーの抑制
Section titled “型エラーの抑制”単一行の特定の診断を抑制するには、行末に # ty: ignore[<rule>] コメントを追加します。例えば:
value: int = "not an int" # ty: ignore[invalid-assignment]プロジェクト全体で型チェックの動作を設定するには、プロジェクトの pyproject.toml に [tool.ty] セクションを追加します。利用可能なオプションについては、ty 設定リファレンスを参照してください。
Python プロジェクトはリントに Ruff を使用します。
リンターの実行
Section titled “リンターの実行”プロジェクトをチェックするためにリンターを呼び出すには、lint ターゲットを実行します。
pnpm nx lint <project-name>yarn nx lint <project-name>npx nx lint <project-name>bunx nx lint <project-name>リント問題の修正
Section titled “リント問題の修正”リントやフォーマットの問題の大部分は自動的に修正できます。--configuration=fix 引数を指定して実行することで、Ruff にリント問題を修正させることができます。
pnpm nx lint <project-name> --configuration=fixyarn nx lint <project-name> --configuration=fixnpx nx lint <project-name> --configuration=fixbunx nx lint <project-name> --configuration=fix同様に、ワークスペース内のすべてのパッケージのすべてのリント問題を修正したい場合は、以下を実行できます:
pnpm nx run-many --target lint --all --configuration=fixyarn nx run-many --target lint --all --configuration=fixnpx nx run-many --target lint --all --configuration=fixbunx nx run-many --target lint --all --configuration=fixリント問題のスキップ
Section titled “リント問題のスキップ”開発中にリント問題によって作業が遅くなるのを避けるため(特にプロジェクトに自動修正できない問題がある場合)、skip-lint 設定でビルドを実行できます:
pnpm nx run-many --target build --configuration=skip-lintyarn nx run-many --target build --configuration=skip-lintnpx nx run-many --target build --configuration=skip-lintbunx nx run-many --target build --configuration=skip-lintこれにより、ビルドの一部として Ruff は実行されますが、lint ターゲットは常に成功したと見なされます。