I have a flask web application, but I get a module not found error when I am importing modules from my own app:
Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/Users/joey/projects/app/app/__main__.py", line 1, in <module> from app import serveModuleNotFoundError: No module named 'app'
What is going wrong here? My __main__.py
:
from app import serveserve()
My __init__.py
:
from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyfrom flask_migrate import Migrateapp = Flask(__name__)class Config(object): UPLOAD_DIRECTORY = app.root_path +"/uploads/" SQLALCHEMY_DATABASE_URI = "postgresql:///app_db" SECRET_KEY = "secret key"app.config.from_object(Config)db = SQLAlchemy(app)migrate = Migrate(app, db)from .routes import home_page, download_page, upload_page, favicon_pageapp.register_blueprint(home_page)app.register_blueprint(download_page)app.register_blueprint(upload_page, url_prefix="/upload")app.register_blueprint(favicon_page)import app.routes as routesimport app.models as modelsdef serve(): return app.run(host="0.0.0.0", port=1212, debug=True)
And my file tree:
app├── Dockerfile├── LICENSE├── README.md├── requirements.txt├── service_entrypoint.sh└── app├── __init__.py├── __main__.py├── __pycache__│ ├── __init__.cpython-312.pyc│ ├── __main__.cpython-312.pyc│ ├── app.cpython-312.pyc│ ├── models.cpython-312.pyc│ ├── routes.cpython-312.pyc│ └── utils.cpython-312.pyc├── app.py├── files.json├── migrations│ ├── README│ ├── __pycache__│ │ └── env.cpython-312.pyc│ ├── alembic.ini│ ├── env.py│ ├── script.py.mako│ └── versions│ ├── 660c5066c59c_files_table.py│ └── __pycache__│ └── 660c5066c59c_files_table.cpython-312.pyc├── models.py├── routes.py├── static│ └── images│ ├── favicon-32x32.png│ ├── favicon.ico│ └── upload.png├── templates│ ├── base.html│ ├── download.html│ ├── index.html│ ├── upload.html│ └── upload_sucess.html├── uploads│ ├── 13879f7a_routes.py│ ├── 9bd5c043_files.json│ ├── b5459a5d_models.py│ └── file.txt└── utils.py
Am I doing anything wrong here? I would appreciate any help. Thanks for taking a look at this question.