파이썬 프로젝트
파이썬 프로젝트 생성기는 현대적인 Python 라이브러리나 애플리케이션을 생성하는 데 사용할 수 있습니다. 이 생성기는 UV로 관리되는 단일 락파일과 UV 워크스페이스 내 가상 환경, 테스트 실행을 위한 pytest, 정적 분석을 위한 Ruff가 포함된 모범 사례 기반 구성으로 설정됩니다.
사용 방법
파이썬 프로젝트 생성
새 파이썬 프로젝트를 두 가지 방법으로 생성할 수 있습니다:
- 설치 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 | - | Project name. |
directory | string | packages | Parent directory where the project is placed. |
projectType 필수 | string | application | Project type |
moduleName | string | - | Python module name |
생성기 출력 결과
생성기는 <directory>/<name>
디렉토리에 다음 프로젝트 구조를 생성합니다:
디렉터리<module-name>
- __init__.py 모듈 초기화
- hello.py 예제 파이썬 소스 파일
디렉터리tests
- __init__.py 모듈 초기화
- conftest.py 테스트 설정
- test_hello.py 예제 테스트
- project.json 프로젝트 설정 및 빌드 타겟
- pyproject.toml UV용 패키징 설정 파일
- .python-version 프로젝트 파이썬 버전 정보
워크스페이스 루트에는 다음 파일들이 생성/업데이트됩니다:
- pyproject.toml UV용 워크스페이스 레벨 패키징 설정
- .python-version 워크스페이스 파이썬 버전 정보
- uv.lock 파이썬 의존성 락파일
파이썬 소스 코드 작성
<module-name>
디렉토리에 파이썬 소스 코드를 추가하세요.
다른 프로젝트에서 라이브러리 코드 임포트
UV 워크스페이스가 설정되어 있으므로, 워크스페이스 내 다른 파이썬 프로젝트에서 현재 프로젝트를 참조할 수 있습니다:
from "my_library.hello" import say_hello
위 예시에서 my_library
는 모듈 이름, hello
는 hello.py
파일, say_hello
는 hello.py
에 정의된 메서드입니다.
의존성 관리
프로젝트에 의존성을 추가하려면 파이썬 프로젝트에서 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
을 업데이트합니다.
런타임 코드 번들링
파이썬 프로젝트를 런타임 코드(예: AWS 람다 함수 핸들러)로 사용할 경우 소스 코드와 의존성을 번들로 만들어야 합니다. 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 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"] }, },}
빌드
project.json
에 정의된 build
타겟을 실행하여 프로젝트를 빌드할 수 있습니다:
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가 프로젝트 테스트를 위해 설정되어 있습니다.
테스트 작성
테스트는 프로젝트 내 test
디렉토리의 test_
접두사가 붙은 파이썬 파일에 작성해야 합니다:
디렉터리my_library
- hello.py
디렉터리test
- 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 문서를 참조하세요.
테스트 실행
테스트는 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'
린팅
파이썬 프로젝트는 Ruff를 사용하여 린트를 수행합니다.
린터 실행
프로젝트 린트 검사를 위해 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
린트 문제 수정
대부분의 린트/포맷팅 문제는 자동으로 수정 가능합니다. --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