question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

aws-cdk: init python is not working

See original GitHub issue

Describe 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

  1. Create a folder titled aws-cdk
  2. Run cdk init --language python inside the aws-cdk
  3. Run source .venv/bin/activate to go into virtual environment
  4. Run pip install -r requirements.txt to install packages
  5. 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:closed
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
MrArnoldPalmercommented, Jul 6, 2022

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 !

1reaction
jashgalacommented, Jul 6, 2022

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

  1. Run cdk init --language python inside the aws-cdk
  2. Run source .venv/bin/activate to go into virtual environment
  3. Run pip install -r requirements.txt to install packages
  4. Run cdk synth to generate Cloud formation, but this step got error like this
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found