Skip to content

Using uv for python requirements broken #13346

@joshorr

Description

@joshorr

Issue description

When using uv to manage python requirements, serverless framework exports the requirements incorrectly. This causes the docker command that builds the dependencies to fail with this error:

ERROR: Directory '.' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.

I traced this back to how uv by default includes the current project as an editable installable via -e . as the first line of the requirements.txt export file to produces.

Serverless framework filters/removes the -e after getting the export from uv, so the final requirements.txt file has just a . in it for the first line.

This causes an install error, because when building the requirements via docker it can't find the project files. The only files in the current directory that the command runs in is the requirements.txt file its self. I think it's expected to not have the project files there, so that's not the issue. The issue is the -e . that is exported in the file.

Here are the options that serverless framework uses to export the requirements:

    await spawn(
      'uv',
      ['export', '--no-dev', '--frozen', '--no-hashes', '-o', outPath],
      { cwd: moduleProjectPath },
    )

I looked at the options and found --no-emit-project option. Using this prevents the -e . from being included in the requirements.txt output.

(as a side-note, may also want to include --no-annotate to remove extra unneeded comments too).

I have attached a minimal reproduction of the issue:

uv-issue-minimal.zip

If you run yarn install and then yarn sls package, you'll get this output:

Packaging "minimal" for stage "defaultenv" (us-west-2)

[!] Stripping -e flag from requirement .

✖ Running "docker run {...docker command-here...}" failed.
Using CPython 3.12.12 interpreter at: /var/lang/bin/python
error: /var/task does not appear to be a Python project, as neither `pyproject.toml` nor `setup.py` are present in the directory
Command output: no checksums to verify
installing to /root/.local/bin
  uv
  uvx
everything's installed!

To add $HOME/.local/bin to your PATH, either restart your shell or run:

    source $HOME/.local/bin/env (sh, bash, zsh)
    source $HOME/.local/bin/env.fish (fish)

error Command failed with exit code 1.

Context

Serverless ϟ Support
This generates a report from your last Serverless Framework command (including any errors) to use for Github Issues, debugging w/ AI, or creating a support ticket w/ Serverless Inc.
Summary Report -----------------

Service Overview

  • Serverless Framework Version: 4.31.2
  • Service Config File: serverless.yml
  • Service Name: minimal
  • Service Runtime: python3.12
  • Service Stage: defaultenv
  • Service Region: us-west-2
  • Command: package --verbose true

Service Path

/Users/josho/src/z/repo-other/uv-issue-minimal/serverless.yml

Error Message

Running "docker run --rm -v /Users/josho/Library/Caches/serverless-python-requirements/b497f785821a182f77bed952ada3aef55d32e327724f1e555e7fdc234a27bb43_x86_64_slspyc:/var/task:z -v /Users/josho/.ssh/id_rsa:/root/.ssh/id_rsa:z -v /Users/josho/.ssh/known_hosts:/root/.ssh/known_hosts:z -v /private/tmp/com.apple.launchd.r46phyCoCY/Listeners:/tmp/ssh_sock:z -e SSH_AUTH_SOCK=/tmp/ssh_sock -v /Users/josho/Library/Caches/serverless-python-requirements/downloadCacheslspyc:/var/useDownloadCache:z -u 0 public.ecr.aws/sam/build-python3.12:latest-x86_64 /bin/sh -c /bin/sh -c 'curl -LsSf https://astral.sh/uv/install.sh | sh' && source ~/.local/bin/env && uv pip install --reinstall-package minimal --target /var/task/ -r /var/task/requirements.txt --cache-dir /var/useDownloadCache && find /var/task -name \*.so -exec strip \{\} \;" failed.
Error: WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
downloading uv 0.10.2 x86_64-unknown-linux-gnu
WARN: The following commands are shadowed by other commands in your PATH: uv uvx
Using CPython 3.12.12 interpreter at: /var/lang/bin/python
error: /var/task does not appear to be a Python project, as neither `pyproject.toml` nor `setup.py` are present in the directory
Command output: no checksums to verify
installing to /root/.local/bin
  uv
  uvx
everything's installed!
To add $HOME/.local/bin to your PATH, either restart your shell or run:
    source $HOME/.local/bin/env (sh, bash, zsh)
    source $HOME/.local/bin/env.fish (fish)

Error Code

PYTHON_REQUIREMENTS_DOCKER_COMMAND_FAILED

Service Config

service: minimal
useDotenv: true
frameworkVersion: "4"
provider:
  name: aws
  runtime: python3.12
  deploymentMethod: direct
  region: ${opt:region, 'us-west-2'}
  stage: ${opt:stage, 'defaultenv'}
custom:
  prune:
    automatic: true
    includeLayers: true
    number: 1
  pythonRequirements:
    dockerizePip: true
    installer: uv
    dockerSsh: true
    slim: true
functions:
  app:
    handler: minimal.handler
    timeout: 30
    memorySize: 1769
    events:
      - schedule:
          rate: cron(*/2 * ? * * *)
          enabled: true

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions