title: Handling relative imports with Flask, gunicorn, and Docker
body: <p>Similar questions have been asked but I've never found a working solution to this issue.</p>
  <p>I have a very basic Docker and Flask setup as such:</p>
  <p>directory structure:</p>
  <pre><code>├── Dockerfile
  ├── app
  │   ├── __init__.py
  │   └── constants.py
  ├── docker-compose.yml
  └── requirements.txt
  </code></pre>
  <p>Contents of <strong>init</strong>.py</p>
  <pre><code>from flask import Flask
  
  from .constants import MYCONSTANT
  
  
  def create_app():
      &quot;&quot;&quot;Create the app.&quot;&quot;&quot;
      app = Flask(__name__)
      @app.route('/', methods=['GET'])
      def index():
          return 'hello world' 
  
      return app
  </code></pre>
  <p>Dockerfile</p>
  <pre><code>FROM python:3.6-alpine
  COPY requirements.txt /
  RUN pip install Flask gunicorn
  COPY app/ /app
  WORKDIR /app
  EXPOSE 8000
  CMD gunicorn --bind=0.0.0.0:8000 &quot;app.__init__.create_app()&quot;
  </code></pre>
  <p>docker-compose.yml</p>
  <pre><code>docker-compose:
  
  version: '3'
  services:
    web:
      build: .
      ports: ['5000:5000']
      volumes: ['./app:/app']
  </code></pre>
  <p>When I run <code>docker-compose up --build</code>, the app complains that module 'app' cannot be found:</p>
  <p><code>web_1  | ModuleNotFoundError: No module named 'app'</code></p>
  <p>Due to test suites and and some other factors, I can't change to <code>from constants import MYCONSTANT</code>, so I'm not really sure how to resolve this.
  I've tried changing the gunicorn line to <code>CMD gunicorn --bind=0.0.0.0:8000 &quot;__init__:create_app()&quot;</code> but then I get this import error:</p>
  <pre><code>web_1  |     from .constants import MYCONSTANT
  web_1  | ImportError: attempted relative import with no known parent package
  </code></pre>