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

Python submodule import not found #3078

Closed
1 of 5 tasks
jsteinich opened this issue Oct 19, 2021 · 4 comments
Closed
1 of 5 tasks

Python submodule import not found #3078

jsteinich opened this issue Oct 19, 2021 · 4 comments
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@jsteinich
Copy link
Contributor

🐛 Bug Report

Affected Languages

  • TypeScript or Javascript
  • Python
  • Java
  • .NET (C#, F#, ...)
  • Go

General Information

  • JSII Version: 1.39
  • Platform: Windows 10. Python 3.8.5

What is the problem?

I'm doing some testing with cdktf version 0.6.4-pre.375. This includes a recent change to generate submodules for the aws provider. This works fine until runtime when the following error is produced:

Traceback (most recent call last):
      File "main.py", line 4, in <module>
        from imports.aws import AwsProvider
      File "C:\project\imports\aws\__init__.py", line 3869, in <module>
        import aws.access_analyzer
    ModuleNotFoundError: No module named 'aws'

The __init__.py file contains at the end a list of imports for all of the submodules. i.e. import aws.access_analyzer. This matches the behavior introduced with #3049. However, the aws module is being defined within that same file and doesn't seem to be referenceable at that point.

I manually updated the file with the imports replaced with a slightly different form: from ..aws import access_analyzer and everything seems to be running just fine.

Repro steps

  1. Install latest cdktf (npm install -g cdktf-cli@next)
  2. Clone https://github.com/hashicorp/terraform-cdk/tree/main/examples/python/aws
  3. Populate cdktf.json terraformProviders with "aws@~> 3" (May also happen in v2)
  4. Run cdktf get
  5. Run cdktf synth and observe error

Other details

We've been having some issues reproducing this and similar issues in different environments. I haven't tracked down the source of difference yet, but I'm wondering if various python / virtualenv differences could be at play here.

@skorfmann
Copy link
Contributor

I'll create a test repo to reproduce this on a small case. In a nutshell, this is causing issues for cdktf in scenarios where users are generating their own code bindings for a Terraform provider or module. The generated code ends up in user configured output directories, by default imports/aws/... for the AWS provider.

This is essentially a breaking change for existing Python users of the AWS provider (the only one with namespaces so far) in cdktf which are generating the code bindings themselves. For this reason, we locked jsii to 1.37.0 in our last release and can't upgrade to newer versions until we found a solution or at least a workaround.

One workaround which came to my mind is rather ugly: Configure jsii to output a imports.aws (where imports would be the user configured output directory) module and skip the generated imports folder when copying it to the final output folder. I'm curious if there are better ideas which are coming to your mind.

Side note: I'm assuming that's not an issue for us when generating packages, since it should be working similar to the aws-cdk-lib package. Gonna verify this, though.

mergify bot pushed a commit that referenced this issue Nov 18, 2021
The goal of this PR is, to find a way for generated Python code to work with namespaced modules regardless of the way it is consumed. While the current implementation of submodule imports seem to work fine when distributed as packages, it's breaking apart when the generated code is imported relatively as part of a single code base. That's what we do in cdktf and the reason why we have locked jsii to `1.37.0` at the moment.

The general idea is, to generate relative imports rather than fqn imports in Python. This could be a potential way to address #3078

```python
# Before    
import jsii_calc.submodule
import jsii_calc.submodule.nested_submodule
import jsii_calc.submodule.nested_submodule.deeply_nested

# After
from . import submodule
from .submodule import nested_submodule
from .submodule.nested_submodule import deeply_nested
```

Besides from a few snapshots being changed, it seems like the tests are still working with this change. However, I'm not sure  about the general implications. So, this entire PR is really meant to be a conversation starter. 

I have confirmed that an import like `import . from some_aws_namespace` would work for us in cdktf.

## References

- #3078
- #3049
- hashicorp/terraform-cdk#1152
- hashicorp/terraform-cdk#1288

---

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

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
@skorfmann
Copy link
Contributor

Fixed with #3181

@NGL321
Copy link
Contributor

NGL321 commented Jan 7, 2022

Hmm, looks like this should have been autoclosed. Closing out now due to resolution.

If this is in error, please feel free to reopen

@NGL321 NGL321 closed this as completed Jan 7, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2022

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

3 participants