aws-cdk: init python is not working
See original GitHub issueDescribe the bug
I just generate a init python cdk template and run the commands from README, but it’s not working at all.
Expected Behavior
Synthesize the CloudFormation template from the init code from CDK
Current Behavior
Got this error message
Traceback (most recent call last):
File "/project/aws-cdk/app.py", line 6, in <module>
from aws_cdk.aws_cdk_stack import AwsCdkStack
File "/project/aws-cdk/aws_cdk/aws_cdk_stack.py", line 1, in <module>
from aws_cdk import (
ImportError: cannot import name 'Stack' from 'aws_cdk' (/project/aws-cdk/aws_cdk/__init__.py)
Subprocess exited with error 1
Reproduction Steps
- Create a folder titled
aws-cdk
- Run
cdk init --language python
inside theaws-cdk
- Run
source .venv/bin/activate
to go into virtual environment - Run
pip install -r requirements.txt
to install packages - Run
cdk synth
to generate Cloud formation, but this step got error like this
Possible Solution
The folder name is the same as the CDK python package naming. It makes python get the wrong folder. So I changed the folder name which CDK generates, then it was fixed.
I think the cdk init
should fix this.
Additional Information/Context
No response
CDK CLI Version
2.30.0
Framework Version
No response
Node.js Version
18.4.0
OS
macOS 12.4
Language
Python
Language Version
Python (3.10)
Other information
No response
Issue Analytics
- State:
- Created a year ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Working with the AWS CDK in Python
cdk init uses the name of the project folder to name various elements of the project, including classes, subfolders, and files. Hyphens in...
Read more >CDK init app unsuccessful even after root install
I have been able to initialise cdk projects in empty directories before without issue. AWS Collective. python · npm · aws-cdk · Share....
Read more >AWS CDK and Python | Step by Step Tutorial
You can now deploy new resources, make changes to existing infrastructure, or clean up resources using CDK similar to working with AWS ......
Read more >cdk init | AWS CDK Workshop
Applying project template sample-app for python Initializing a new git repository... Executing Creating virtualenv... # Welcome to your CDK Python project!
Read more >Lessons in AWS Python CDK: 1-Getting Started
I am going to be installing AWS CDK v2 and not v1. ... mkdir <project-name> cd <project-name> cdk init app --language python.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah this is essentially a naming collision as the local python module being named the same as the aws_cdk package prevents you from correctly importing without aliasing the dependency to something else. I’m not sure if there is a simple workaround if you really wanted to name your python package
aws_cdk
(like aliasing the dependency to something else) but for now using a different directory/package name is the best solution.ty @jashgala !
I believe you are right, when I initialize the Python CDK inside a folder named “aws-cdk” it results in the same failure like the one that you mentioned. I’m think this might be an expected behavior and not a bug, as this problem happens due to the use of the name of aws-cdk module for a python project that internally references the same.
Nevertheless, I would allow someone who is more proficient in Python to confirm this. Meanwhile, you might want to update the steps to reproduce the issue accordingly:
Reproduction Steps* 0. Create a folder titled
aws-cdk
cdk init --language python
inside theaws-cdk
source .venv/bin/activate
to go into virtual environmentpip install -r requirements.txt
to install packagescdk synth
to generate Cloud formation, but this step got error like this