0

I'm getting a ModuleNotFoundError while running my flask app with this dir structure:

project
     | app
          | __init__.py
          | urls.py
          | views.py
     | __main__.py

In the project dir, while running python3 ., I'm getting a

File "./app/__init__.py", line 4, in <module>
    import urls
ModuleNotFoundError: No module named 'urls'

The content of __init__.py is

#!/usr/bin/env python3
import logging
import flask_cors
import urls
from views import flask_app

log = logging.getLogger("werkzeug")
log.disabled = True
flask_cors.CORS(flask_app)

def run(host, port):
    flask_app.run(host=host, port=port)

The code is a little big so this is the link to it.

Lakshaya U.
  • 1,089
  • 1
  • 5
  • 21

1 Answers1

1

If you want to import other files in the same directory in a __init__.py file, you need to use relative import syntax:

from . import urls

This way Python will look for urls.py alongside your __init__.py, instead of searching in sys.path.

iBug
  • 35,554
  • 7
  • 89
  • 134