Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsii-pacmak): different packages sharing a namespace fail in Bazel #4437

Merged
merged 2 commits into from
Mar 4, 2024

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Mar 1, 2024

In Bazel, every package is extracted into a separate directory that are all put into the $PYTHONPATH individually. However when searching, Python will latch onto the directory that has an __init__.py in it and only import from that directory.

Example:

eco_package1/
└── site-packages/
    └── eco/
        └── package1/
              └── __init__.py

eco/
└── site-packages/
    └── eco/
        └── __init__.py

In this case, the following command will fail:

import eco.package1

Because eco/package1/__init__.py is not in the same directory as eco/__init__.py.

We can fix this by generating a command into every __init__.py that uses pkgutil to extend the search path for a package; it will put all directories in $PYTHONPATH that contain the same package onto the search path for submodules. The command looks like this:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

Resolves #3881, thanks to @psalvaggio-dl for providing a repro and doing the research for a fix.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

In Bazel, every package is extracted into a separate directory that are
all put into the `$PYTHONPATH` individually. However when searching,
Python will latch onto the directory that has an `__init__.py` in it and
only import from that directory.

Example:

```
eco_package1/
└── site-packages/
    └── eco/
        └── package1/
              └── __init__.py

eco/
└── site-packages/
    └── eco/
        └── __init__.py
```

In this case, the following command will fail:

```py
import eco.package1
```

Because `eco/package1/__init__.py` is not in the same root directory as
`eco/__init__.py`.

We can fix this by generating a command into every `__init__.py` that
uses `pkgutil` to extend the search path for a package; it will put
all directories in `$PYTHONPATH` that contain the same package onto
the search path for submodules. The command looks like this:

```py
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
```
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Mar 1, 2024
Copy link
Contributor

mergify bot commented Mar 4, 2024

Thank you for contributing! ❤️ I will now look into making sure the PR is up-to-date, then proceed to try and merge it!

@mergify mergify bot added the pr/ready-to-merge This PR is ready to be merged. label Mar 4, 2024
Copy link
Contributor

mergify bot commented Mar 4, 2024

Merging (with squash)...

@mergify mergify bot merged commit 5c0d9e4 into main Mar 4, 2024
30 checks passed
@mergify mergify bot deleted the huijbers/python-bazel branch March 4, 2024 10:17
@mergify mergify bot removed the pr/ready-to-merge This PR is ready to be merged. label Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws_cdk): Py module error when using Bazel
2 participants