'Application says module not found after moving to directory

I'm rather new to python and have this odd issue, which I can't seem to find an answer for.

When both app.py and mod_db were in the root directory, it works but when I shifted them to a sub directory

My directory structure:

demo_api
|
|-- demo-api
    |
    |-- __init__.py
    |-- app.py
    |-- mod_db.py

My main module

import json

from flask import Flask, request, Response
from .db_mod import insert_and_score

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/emotional_scoring', methods=['POST'])
def get_scoring():
    json_obj = request.json
    ret_json = insert_and_score(json_obj)
    resp = Response(json.dumps(ret_json), mimetype='application/json', status=200)
    return resp


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False)

The error message is on this line

from .mod_db import insert_and_score

I've tried change the sub-directory name. I've tried doing a full path, i.e. from demo_app.mod_db import insert_and_score and the error is ModuleNotFoundError: No module named 'demo_app'

The issue is that it works find in my IDE (PyCharm) but when I do it on command line, these are the errors I encounter.



Solution 1:[1]

As stated by mfrackowiak, I just had to change it to

from db_mod import insert_and_score

and it worked. But PyCharm doesn't like this. So I think it might be a PyCharm issue.

Solution 2:[2]

Fix by removing relative import

It seems you want both the relative imports and Pycharm to be happy. Like mfrackowiak said, you want to use absolute importing.

from db_mod import insert_and_score

Tell PyCharm what the new "Sources Root" is

Now to make Pycharm happy, you will want to tell it that the demo_api subdirectory is a "Sources Root". You can do this by right clicking the directory and go to Mark Directory as > Sources Root. You can also find it in Preferences > Project Structure. You can do this for each subdirectory, as needed.

Why this is good to do

This is useful when you have a python project as a subdirectory of a repo with many configs, scripts, and other files and directories. You often don't want the python app to be mixed in with those, so you move it to an app/ folder. This confuses Pycharm, as by default, it uses the Content Root as the Sources Root as well. You can fix this by simply telling Pycharm explicitly what the Sources Root is.

Example:

my-awesome-project/ <---- Content Root
|--.venv/
|  |--<venv stuff>
|--scripts/
|  |--build.sh
|  |--run.sh
|--docker/
|  |--dev.Dockerfile
|  |--prod.Dockerfile
|--app/ <---------------- Sources Root
|  |--sub-mod1/
|  |  |--foo.py
|  |  |--bar.py
|  |--sub-mod2/
|     |--baz.py
|--.gitignore
|--.python-version
|--requirements.txt
|--dev.env
|--docker-compose.yml

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2