5

I have a python application that I am trying to deploy with zappa. The root level of my directory has the application and a directory named helper. The structure looks like this:

|-app.py
|-zappa_settings.json
|-helper
   |-api.py
   |-__init.py__

Within the helper directory there is an api.py file that is referenced in my app.py like so

from helper import api

When I run the command to package and deploy using zappa deploy dev it will not bundle the helper directory in the deployment, only the root level application directory. How do you tell zappa to include all sub-directories when packaging and deploying?

medium
  • 3,708
  • 14
  • 50
  • 63

1 Answers1

5

After struggling with this myself, I realized that the idea is to package up your other code, install it in your virtual environment, and have app.py just be a driver that calls your other functions.

Here's a concrete minimum example using Flask. First, let's extend your example with one more file, setup.py:

|-app.py
|-zappa_settings.json
|-setup.py
|-helper
   |-api.py
   |-__init.py__

__init__.py is empty. The rest of the files are as follows:

# setup.py
from setuptools import setup

setup(
    name='helper',
    packages=['helper'],
    include_package_data=True,
    install_requires=['flask']
)


# app.py    
from helper import api
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return api.index()


# helper/api.py
def index():
    return "This is the index content"


# zappa_settings.json
{
    "dev": {
        "app_function": "app.app",
        "s3_bucket": "my_bucket"
    }
}

Now you pip install -e . while in your virtual environment. If you now run app.py using Flask and go http://127.0.0.1:5000/, you'll see that you get This is the index content. And if you deploy using Zappa, you'll see that your API endpoint does the same thing.

David Bruce Borenstein
  • 1,323
  • 1
  • 13
  • 31
  • What I have not figured out how to do is to pass the `app` context to `api.py` so that I can do the routing without losing those beautiful decorators. If you can live without decorators, you can do it like this: https://stackoverflow.com/questions/17129573/can-i-use-external-methods-as-route-decorators-in-python-flask – David Bruce Borenstein Mar 12 '18 at 01:26