[Python] ModuleNotFoundError: No module named 'aws_cdk'

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use aws_cdk in my project. I am using visual studio as a IDE and I setup my virtualenv doing this:

    1. Install https://nodejs.org/en/.
    2. Through npm, install cdk from cmd window: npm install -g aws-cdk
    3. Install python3
    4. Using terminal in Visual Studio Code: go to the project folder
    5. Run python -m venv .venv to create the virtual env.
    6. Activate it: source .venv/bin/activate
    7. Run pip install: pip install -r requirements.txt
    8. Run another pip install: pip install -r requirements-dev.txt.
    9. Check with cdk diff in the cmd

    This last step returns:

    Traceback (most recent call last):
    File "C:......\app.py", line 4, in <module>
    import aws_cdk as cdk
    ModuleNotFoundError: No module named 'aws_cdk'


    My App.py looks like this:

    #!/usr/bin/env python3
    import os

    import aws_cdk as cdk

    import module_config

    from myproject.myproject_stack import MyStack

    app = cdk.App()

    tags = module_config.with_tags(service_name="my_project")
    prefix = module_config.with_prefix(service_name="my_project")
    MyStack(
    app,
    construct_id=prefix,
    prefix=prefix,
    tags=tags,
    env=module_config.env,
    )
    app.synth()


    My requirements.txt looks like:

    aws-cdk-lib==2.56.1
    constructs>=10.0.0,<11.0.0


    The requirement-dev.txt is:

    bandit>=1.7.4
    black>=22.10.0
    coverage>=6.4.4
    pylint==2.15.5
    pytest
    pytest-cov
    yamllint
    pre-commit


    If in the vistual studio I do ctrl+mouse click it opens the aws-cdk code, therefore I know it is installed, but seems like the virtual env is not able to find it. In my local repository, the folder .venv has a folder Lib\site-packages and this one has another called aws_cdk.

    I see everything corret, but when running the cdk diff it breaks.

    Continue reading...

Compartilhe esta Página