Mervin Hemaraju
Mervin Hemaraju

Reputation: 2117

Python ModuleNotFoundError: No Module named 'app'

I have a Flask app where I am having issues to run locally on my Windows machine.

Whenever i try to run it in my venv, i get the error ModuleNotFoundError: No Module named 'app'

My project hierarchy is as follows:

├── app
│   ├────────── __init__.py
│   ├────────── v0_1
│   └────────── v0_2
├── README
├── run.py
└── tests

app/init.py

from flask import Flask
import app.v0_1.v0_1 as V0_1
import app.v0_2.v0_2 as V0_2
import app.messages.messages as MSG

# Create a Flask Application
app = Flask(__name__)

run.py

from app import app

if __name__ == "__main__":
    app.run(host="0.0.0.0")

.vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "python": "${config:python.pythonPath}"
        }
    ]
}

.vscode/settings.json

{
    "python.linting.flake8Enabled": false,
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "python.pythonPath": "venv\\Scripts\\python.exe"
}

Each folder has their own init.py

When i build a Docker file and run my app, it works fine. When i try to run it locally i encounter the error mentioned above. The error points to these two files in app/__init__.py :

import app.v0_1.v0_1 as V0_1
import app.v0_2.v0_2 as V0_2

These folders contain blueprints of my APIs

Here is a link of the full project on GitHub here

Can someone please help me ?

Upvotes: 1

Views: 16910

Answers (1)

shrewmouse
shrewmouse

Reputation: 6020

It's in __init__.py. There is no module app inside /app/. The only modules are v0_1 and v0_2. Get rid of the app prefix.

import v0_1.v0_1 as V0_1
import v0_2.v0_2 as V0_2

should be

from v0_1 import v0_1 as V0_1
from v0_2 import v0_2 as V0_2

Here is my layout

[root@sri-0000-0001 sandbox]# tree
.
├── app
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── v0_1.py
│   ├── v0_1.pyc
│   ├── v0_2.py
│   └── v0_2.pyc
└── run.py

Here are the files:

run.py

from app import app

if __name__ == "__main__":
    app.run(host="0.0.0.0")

__init__.py

from flask import Flask
from v0_1 import v0_1 as V0_1
from v0_2 import v0_2 as V0_2

# Create a Flask Application
app = Flask(__name__)

v0_1.py

v0_1=None

v0_2.py

v0_2=None

Upvotes: 2

Related Questions