20

When I run cdk deploy, I get the following error:

Traceback (most recent call last):
  File "app.py", line 3, in <module>
    from aws_cdk import core
ModuleNotFoundError: No module named 'aws_cdk'

I installed cdk with npm

npm install -g aws-cdk

I activated the virtual env by

source .env/bin/activate

I'm using python3.8. I installed aws_cdk dependencies by

pip install -r requirements.txt

When editing the python files, I am able to import aws_cdk and run individual functions successfully. I think the problem is that cdk is located in the /usr directory:

> which cdk
/usr/local/bin/cdk

And I think it's using python from my /usr/bin instead of virtual env. How do I make cdk use python in my virtual environment?

EDIT:

requirements.txt

-e .
MoneyBall
  • 2,343
  • 5
  • 26
  • 59
  • Can you please share your requirements.txt file? – Stefan Freitag Jul 29 '20 at 20:18
  • Thanks for adding the requirements.txt. I setup a python project from scratch with CDK 1.55.0. Executed also the steps you mentioned (as they are also shown when creating a new python project with the cdk) and was able to run `cdk synth` and `cdk deploy` without any issue. My python version is 3.8.2 and the cdk is available under `/usr/local/bin/cdk` Can you try to activate the virtual environment, do a `cdk synth` and then do a `which python`? – Stefan Freitag Jul 31 '20 at 18:42
  • @StefanFreitag which python points to `/.env/bin/python`. I think something went wrong with the `npm` installation. If I use `/usr/bin/python`, everything works fine, but when it points to my `.env/bin/python`, I get that error. – MoneyBall Aug 01 '20 at 02:21
  • The `.env/bin/python` is the one that is working for me and with the `/usr/bin/python` I get the error about the missing module. If it is the other way around for you I am running out of ideas. – Stefan Freitag Aug 01 '20 at 20:27
  • 1
    Faced the same issue. Doing an `rm -rf` on existing `venv` and then re-creating it resolved the problem. – narayan Jan 20 '21 at 12:37

10 Answers10

12

TLDR; .env/bin/pip3.8 install -r requirements.txt solved this same issue at my end

Once you run source .env/bin/activate it actually looks for python libraries on .env/lib/python3.8/site-packages (at least for me!). Thus running cdk ls which calls app.py will looks for aws_cdk in venv site package, not from system site-package as long as "include-system-site-packages = false" under .env/pyvenv.cfg

bigbro
  • 163
  • 1
  • 7
8

I think you have to order

python -m pip install -r requirements.txt

This will install the standard dependencies for your CDK project.

P. Str
  • 580
  • 1
  • 5
  • 18
3

You have to re-install the aws_cdk in virutal Env. I hope its works

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
3

install via cmd as below.

pip install aws-cdk.core

or

add following line in requirements.txt file

aws-cdk.core

it should resolved

1

Came here with the same issue, but as Geremy hints at another reason. On mac, when brew updates the python version it basically breaks your existing virtual environments. The python version that CDK picks isn't the one you necessarily built the virtual environment with.

inserted this into my app.py to get to the bottom of this:

import sys
print(sys.path)

helped tshoot.

D. Chapman
  • 11
  • 1
1

For CDK v2, the core modules is integrated inside aws_cdk and you do not have to import aws_cdk.core. You may remove it or replace 'aws_cdk.core' with just 'aws_cdk'

JustP
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 14 '22 at 21:09
0

I got this error when I moved my directory structure around. I was able to fix it by editing .venv/bin/activate and updating the value of the VIRTUAL_ENV env var.

Geremy
  • 2,415
  • 1
  • 23
  • 27
0

python -m pip install aws-cdk-lib

0

I ran in to a similar problem which was fixed when I re-ran npm install. I see that you did that, however maybe running it again might help now, as it did for me.

Michael Behrens
  • 911
  • 10
  • 8
-1

You should run it in virtual environment.

source .env/bin/activate

then

cdk deploy