Python import modules in same directory for Flask
<p>I am trying to create a Flask application. I would like to include a separate module in my application to separate logic into distinct units. The separate module is called 'validator' and my current directory structure looks like this:</p>

<pre><code>src/
    validation-api/
        __init__.py
        api.py
    validator/
        __init__.py
        validator.py
    validation-form/
        ...
    updater/
        ...
</code></pre>

<p>My Flask application is in <code>api.py</code> and I am trying to do <code>from validator import ValidationOptions, ValidationResult, ValidationRun</code> where <code>ValidationOptions</code>, <code>ValidationResult</code>, and <code>ValidationRun</code> are classes in <code>validator</code>. </p>

<p>I am getting the error </p>

<blockquote>
  <p>ImportError: No module named validator </p>
</blockquote>

<p>If I try <code>from .validator...</code> or <code>from ..validator</code> I get the error </p>

<blockquote>
  <p>ValueError: Attempted relative import in non-package</p>
</blockquote>

<p>I don't quite understand how modules and packages work in Python. Any suggestions?</p>

<hr>

<p>Contents of api.py:</p>

<pre><code>from flask import Flask, request
from validator.validator import ValidationOptions, ValidationResult, ValidationRun

app = Flask(__name__)

@app.route("/validate", methods=["POST"])
def validate(self):
    pass

if __name__ == '__main__':
    app.run(debug=True)
</code></pre>

<p>I am starting Flask using the following three commands:</p>

<pre><code>set FLASK_APP=api
set FLASK_DEBUG=1
python -m flask run
</code></pre>

<p>Thanks to @Daniel-Roseman, I've figured out what's going on. I changed the <code>FLASK_APP</code> environment variable to <code>validation-api.api</code>, and ran the <code>python -m flask run</code> command from <code>src</code>. All imports are working now!</p>

python flask