aws / aws/aws-cdk

(aws-lambda-python-alpha): I'm failing to bundle a Python 3.12 function using pipenv.

Open
#30,170 8 comments 9 reactions 1 assignee Assigned to @Leo10Gama View on GitHub
@aws-cdk/aws-lambda-python bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

When trying to export the Pipfile to requirements.txt, I encounter the following error:
`AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?`

### Expected Behavior

I expect to successfully bundle.

### Current Behavior

```
Traceback (most recent call last):
File "/usr/app/venv/bin/pipenv", line 5, in
from pipenv import cli
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/__init__.py", line 57, in
from .cli import cli
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/cli/__init__.py", line 1, in
from .command import cli # noqa
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/cli/command.py", line 7, in
from pipenv.cli.options import (
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/cli/options.py", line 3, in
from pipenv.project import Project
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/project.py", line 21, in
from pipenv.core import system_which
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/core.py", line 30, in
from pipenv.utils.resolver import venv_resolve_deps
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/utils/resolver.py", line 14, in
from pipenv.vendor.requirementslib import Pipfile, Requirement
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/requirementslib/__init__.py", line 7, in
from .models.lockfile import Lockfile
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/requirementslib/models/lockfile.py", line 14, in
from ..utils import is_editable, is_vcs, merge_items
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/requirementslib/utils.py", line 11, in
import pip_shims.shims
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/__init__.py", line 26, in
from . import shims
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/shims.py", line 12, in
from .models import (
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/models.py", line 775, in
Command.add_mixin(SessionCommandMixin)
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/models.py", line 689, in add_mixin
mixin = mixin.shim()
^^^^^^^^^^^^
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/models.py", line 737, in shim
result = self.traverse(top_path)
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/models.py", line 729, in traverse
result = shim.shim()
^^^^^^^^^^^
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/models.py", line 575, in shim
imported = self._import()
^^^^^^^^^^^^^^
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/models.py", line 600, in _import
result = self._import_module(self.calculated_module_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/vendor/pip_shims/models.py", line 352, in _import_module
imported = importlib.import_module(module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lang/lib/python3.12/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/patched/notpip/_internal/cli/req_command.py", line 15, in
from pipenv.patched.notpip._internal.cache import WheelCache
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/patched/notpip/_internal/cache.py", line 13, in
from pipenv.patched.notpip._internal.exceptions import InvalidWheelFilename
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/patched/notpip/_internal/exceptions.py", line 7, in
from pipenv.patched.notpip._vendor.pkg_resources import Distribution
File "/usr/app/venv/lib/python3.12/site-packages/pipenv/patched/notpip/_vendor/pkg_resources/__init__.py", line 2164, in
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
```

### Reproduction Steps

I'm using the following stack.
```typescript
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { PythonFunction } from '@aws-cdk/aws-lambda-python-alpha';

export class TestStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const entry = 'src'
new PythonFunction(this, 'TestFunction', {
functionName: "testFunction",
runtime: lambda.Runtime.PYTHON_3_12,
entry,
index: 'index.py',
handler: 'handler',
bundling: {
assetExcludes: ['.venv'],
},
});
}
}
```

When I run cdk synth with the Pipfile placed in the src entry point, it results in an error.
```Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"

[dev-packages]

[requires]
python_version = "3.12"
python_full_version = "3.12.2"
```

### Possible Solution

There is the following description in the Dockerfile.

```
# pipenv 2022.4.8 is the last version with Python 3.6 support
pip install pipenv==2022.4.8 poetry==$POETRY_VERSION && \
```

I think it would be fine to upgrade the version of pipenv, as Python 3.6 is no longer supported by Lambda.
The same error was present up to pipenv==2023.3.20, but it seems that from pipenv==2023.4.20 onwards, the error does not occur

### Additional Information/Context

_No response_

### CDK CLI Version

2.141.0 (build 3d1c06e)

### Framework Version

_No response_

### Node.js Version

v18.16.0

### OS

Windows (WSL2)

### Language

TypeScript

### Language Version

Typescript (5.4.5)

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.