How to access Python dependencies when SSH'ing to Paketo image?

40 Views Asked by At

I have an image built using the Paketo Python Buildpack. It executes the command in the Procfile fine including python dependencies. However, when I exec on to the running instance e.g. using ecs exec in order to run an ad-hoc script I've found it doesn't have access to the installed python dependencies e.g.

$ python3 -m scripts.my_script
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/workspace/scripts/my_script", line 4, in <module>
    from app import app
  File "/workspace/app.py", line 1, in <module>
    import connexion
ModuleNotFoundError: No module named 'connexion'

Is it possible to run python in a way which includes the dependencies in the path? This was possible using the /tmp/lifecycle/shell command on Cloudfoundry but is a similar thing possible when using a Paketo image?

1

There are 1 best solutions below

0
On

I have found a workaround. We can set PYTHONPATH manually to find the dependencies. I found the PYTHONPATH using this command:

cat /layers/paketo-buildpacks_pip-install/packages/env/PYTHONPATH.prepend

Then I can set this when executing the script e.g.

PYTHONPATH=/layers/paketo-buildpacks_pip-install/packages/lib/python3.10/site-packages python3 -m scripts.my_script