-
Notifications
You must be signed in to change notification settings - Fork 245
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
Comments
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 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 One workaround which came to my mind is rather ugly: Configure jsii to output a Side note: I'm assuming that's not an issue for us when generating packages, since it should be working similar to the |
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
Fixed with #3181 |
Hmm, looks like this should have been autoclosed. Closing out now due to resolution. If this is in error, please feel free to reopen |
|
🐛 Bug Report
Affected Languages
TypeScript
orJavascript
Python
Java
C#
,F#
, ...)Go
General Information
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:
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, theaws
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
cdktf.json
terraformProviders
with"aws@~> 3"
(May also happen in v2)cdktf get
cdktf synth
and observe errorOther 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.
The text was updated successfully, but these errors were encountered: