Home > Back-end >  Module not found Error while running flask
Module not found Error while running flask

Time:10-02

My application runs fine by itself. But when I run it via flask - It gives me Module not found Error. My project is set up as follows

── myproject
    ├── mypackage
    │   ├── __init__.py
    │   ├── utils.py
    └── main.py
    ├── app.py (flask app)

Import statement for my main.py is as follows:

from myproject.mypackage.utils  import func

Following is my app.py

from flask import Flask, request
import main  

@app.route('/)
def run_job():    
    return main.do_something()

if __name__ == '__main__':
   app.run(debug = True)

I get error

ModuleNotFoundError: No module named myproject

CodePudding user response:

You said

Import statement for my main.py is as follows:

from myproject.mypackage.utils  import func

If you mean that within main.py, you have the command - from myproject.mypackage.utils import func, then you should change it to

from mypackage.utils import func

CodePudding user response:

Relative imports

The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported.

Relative imports make use of dot notation to specify location.

A single dot means that the module or package referenced is in the same directory as the current location. Two dots mean that it is in the parent directory of the current location

So in your case

You should use one dot to access the package in your import

from .mypackage.utils  import func 
  • Related