3

Context

I have a local folder containing multiple Python Flask applications and a commons python package. As you can see I have created a virtualenv for each of the Flask applications, because I would like to deploy to AWS Lambda using Zappa and want to include only the relevant dependencies in the package zip that goes to AWS.

project_folder/
 +-- commons/
 |   +-- __init__.py
 |   +-- setup.py
 |   +-- module1.py
 +-- application1/
 |   +-- __init__.py
 |   +-- app.py
 |   +-- env/
 +-- application2/
 |   +-- __init__.py
 |   +-- app.py
 |   +-- env/

Problem

I am not able to include the commons package in the Flask applications. I assume this has to do because it is collateral and not a subdirectory.

  • I would like to avoid adding a copy into each Flask application.
  • I do not want to create the whole project into one gigantic package because it will get too large for AWS Lambda

Question

How can I make sure the commons package is included when I call zappa deploy from inside project_folder/application1/?

Tom Hemmes
  • 2,000
  • 2
  • 17
  • 23

1 Answers1

2

One of the solutions is to install the package in your virtual environment (pip install . in the commons directory). Zappa would use all the packages that are installed in it.

Of course, this is a bit cumbersome because you'd need to re-install the package every time you modify it. You can either:

  1. Create a script that would help you by re-installing and then calling zappa deploy.
  2. Try to create your own module and register it in the callbacks section of zappa_settings.json. This module would be called by the deployment/update process and could theoretically re-install the package beforehand. However, this is option is just my guess that it might be possible. I've never tried anything like that.
tmt
  • 7,611
  • 4
  • 32
  • 46
  • Thank you for your quick answer! I am afraid that I am not even able to get it to work a single time by just using `pip install .`. The installation of commons runs fine, however when I `cd application1` and run `python -c "import commons"` it will not work. because the import is not in the path. Simply appending the path in the code works locally, but not for the Zappa package. – Tom Hemmes Oct 10 '19 at 16:37
  • 2
    @TomHemmes That's probably because you don't have a valid directory structure and pip actually doesn't find any package to install. Try to put *module1.py* in *commons/mypackage/module1.py*. Then try to install again and check that `mypackage` is installed by running `pip list`. If you don't manage, I'd try to alter the answer by adding a sample of *setup.py*. – tmt Oct 10 '19 at 17:15
  • 1
    That was totally it, how foolish, because I was using `find_packages()` I didn't catch this at all. Thank you so much, this allows me to deploy. Your first option solves the problem, I'll test the second one as well and will report on which works better! – Tom Hemmes Oct 10 '19 at 18:35