CDKTF not recognizing libraries outside of python standard library

839 Views Asked by At

I am unable to run cdktf because cdktf won't work with packages installed from PyPI. I need cdktf to be able to install / access packages installed from PyPI.

$ cdktf diff

⠏  Synthesizing
[2022-11-11T14:03:01.343] [ERROR] default - Traceback (most recent call last):
  File "/Users/jcbcodes/workspace/project/main.py", line 25, in <module>
    from utils.foo import bar
  File "/Users/jcbcodes/workspace/project/utils/foo.py", line 5, in <module>
    import boto3
ERROR: cdktf encountered an error while synthesizing

Synth command: pipenv run python main.py
Error:         non-zero exit code 1

Command output on stderr:

    Traceback (most recent call last):
      File "/Users/jcbcodes/workspace/project/main.py", line 25, in <module>
        from utils.foo import bar
      File "/Users/jcbcodes/workspace/project/utils/foo.py", line 5, in <module>
        import boto3
    ModuleNotFoundError: No module named 'boto3'




⠹  Synthesizing

boto3 is installed in my python virtual env. I also added boto3 to the Pipfile. I am unclear how to install packages listed under [packages] in the Pipfile. My Pipfile contains the following

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[requires]
python_version = "3"

[packages]
cdktf = "~=0.13.3"
pytest = "*"
boto3 = "1.26.6"
2

There are 2 best solutions below

1
On

As you have a Pipfile I'd expect your app command in your cdktf.json looks something like this: pipenv run python main.py. As you can see python is executed through Pipenv. You can use pipenv install to install the dependencies and then use it as you normally would in your python program.

0
On

This problem is because the CDK uses pipenv to manage packages and dependencies. To solve it, run pipenv install <package> for whatever package cause you the problem.

In general, cdktf can only work with the packages in the Pipeile listed like this:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[requires]
python_version = "3"

[packages]
cdktf = "~=0.15.5"
<myPackageName> = "*"

You can also extract a requirements.txt file and install it using pipenv like this: pipenv install -r path/to/requirements.txt