1

I have the following folder structure: enter image description here

In the application.py file I am using a factory method create_app():

# application.py
def create_app():
    flask_app = Flask(__name__)
    return flask_app


app = create_app()

# --- Import Routes ---
from src.routes import *

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

In the test folder I have the following code

# /tests/test.py
from application import create_app


def test_root_route():
    flask_app = create_app()
    with flask_app.test_client() as test_client:
        response = test_client.get('/')
        assert response.status_code == 200
        

Now I get the following error when running the test: from application import create_app ModuleNotFoundError: No module named 'application'

Why can't I import the method from the application.py file?

Unfortunately I can't change the folder structure as the application.py needs to be in root so it can be run by our server.

mmargo
  • 31
  • 1
  • 5

2 Answers2

0

Please share folder structure properly , looking at it currently seems like while running tests.py , the application.py file is in the directory above the current one .

Also you can try to make a test.py where apllication.py is located. Then import the tests you've written in test directory.

BOTMAN
  • 11
  • 3
-1

try using from .application import with and __init__.py file in the root file

Python has a lot of problems with sibling imports

bujian
  • 152
  • 8
  • Unfortunately this does not work. – mmargo Jun 14 '22 at 15:44
  • do you have a `__init__.py` file in your root directory? – bujian Jun 14 '22 at 15:47
  • Yes. I added an ```__init__.py``` file with the content: ```from application import *``` to the root directory. I get the following error: ImportError: attempted relative import with no known parent package – mmargo Jun 14 '22 at 16:11