0

I have an app that I would like to deploy to AWS Lambda and for this reason it has to have Python 3.9.

I have the following in the pyproject.toml:

name = "app"
readme = "README.md"
requires-python = "<=3.9"
version = "0.5.4"

If I try to pip install all the dependencies I get the following error:

ERROR: Package 'app' requires a different Python: 3.11.1 not in '<=3.9'

Is there a way to specify the Python version for this module?

I see there is a lot of confusion about this. I simply want to specify 3.9 "globally" for my build. So when I build the layer for the lambda with the following command it runs:

pip install . -t pyhon/

Right now it has only Python 3.11 packaged. For example:

❯ ls -larth python/ | grep sip
siphash24.cpython-311-darwin.so

When I try to use the layer created this way it fails to load the required library.

Istvan
  • 7,500
  • 9
  • 59
  • 109
  • Does this answer your question? [Multiple Python versions on the same machine?](https://stackoverflow.com/questions/2547554/multiple-python-versions-on-the-same-machine) – Abdul Aziz Barkat Feb 15 '23 at 13:42
  • No that is for something else. – Istvan Feb 15 '23 at 13:51
  • AWS Lambda doesn't require Python 3.9. It also supports 3.7 and 3.8 at the time of writing. I think you're saying that you'd prefer to use a later version (3.11) but Lambda doesn't support it yet out of the box. – jarmod Feb 15 '23 at 13:52
  • 1
    "_How to build a Python project for a specific version of Python?_" the answer to that is to use that specific version. You already seem to have Python 3.11 (Which is why the error) hence the linked question does solve and hence answer your problem. – Abdul Aziz Barkat Feb 15 '23 at 14:08
  • @jarmod do you understand what <=3.9 means? – Istvan Feb 15 '23 at 14:24
  • @jarmod no this is not what I am saying. I am wondering it is is possible to specify a version for a module. The module is called "app" in this case and even though I have requires-python = "<=3.9" in pyproject.toml it still tries to use 3.11 for the module. Is it possible to specify the version for the module? – Istvan Feb 15 '23 at 14:26
  • The first part of my comment relates to your statement: "I would like to deploy to AWS Lambda and for this reason it has to have Python 3.9" but thanks for clarifying things. – jarmod Feb 15 '23 at 14:45
  • Yeah that is the newest version of Python Lambda supports so I would like to go with that is possible. – Istvan Feb 15 '23 at 14:47

1 Answers1

0

There are multiple ways of solving this.

Option 1 (using pip's built in facilities to restrict Python version)

 pip install . \
  --python-version "3.9" \
  --platform "manylinux2010" \
  --only-binary=:all: -t python/

Another way of solving this is with Docker:

FROM python:3.9.16-bullseye

RUN useradd -m -u 5000 app || :
RUN mkdir -p /opt/app
RUN chown app /opt/app
USER app
WORKDIR /opt/app
RUN python -m venv venv
ENV PATH="/opt/app/venv/bin:$PATH"
RUN pip install pip --upgrade
RUN mkdir app
RUN touch app/__init__.py
COPY pyproject.toml README.md ./
RUN pip install . -t python/

This way there is no change to create a layer for AWS Lambda that is newer than Python 3.9.

Istvan
  • 7,500
  • 9
  • 59
  • 109