I'm trying to import app from flask to make unittest, but i have trouble with imports:
Error description: Traceback (most recent call last): File "tests.py", line 4, in from app import app File "/home/master/Workspaces/eduCAT/faq-test/api/app.py", line 4, in from .models import Question, QuestionSchema, Message, MessageSchema ImportError: attempted relative import with no known parent package
I have a file tree with all files in same folder like this:
Folder [api]
-> __init__.py
-> app.py
-> models.py
-> test.py
-> config.py
The api is running well with flask run, but when i try to import app to test.py i'm getting this error.
Imports: app.py
from flask import jsonify, request
from .models import Question, QuestionSchema, Message, MessageSchema
from api import app, db
Imports: __init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
app.config.from_object('api.config')
db = SQLAlchemy(app)
ma = Marshmallow(app)
Imports: models.py
from api import db, ma
And on the test.py
import os
import unittest
from app import app
Imports that I've already tried on test.py : import:
from app import app
error:
Traceback (most recent call last): File "tests.py", line 4, in from app import app File "/home/master/Workspaces/eduCAT/faq-test/api/app.py", line 4, in from .models import Question, QuestionSchema, Message, MessageSchema ImportError: attempted relative import with no known parent package
import:
from .app import app
error:
Traceback (most recent call last): File "tests.py", line 4, in from .app import app ModuleNotFoundError: No module named 'main.app'; 'main' is not a package
import:
from api import app
error:
Traceback (most recent call last): File "tests.py", line 4, in from api import app ModuleNotFoundError: No module named 'api'
Im stucked on this test.
Edit Full code
app.py - https://dpaste.de/JgEi
__init__.py - https://dpaste.de/A9sG
models.py - https://dpaste.de/xEx4
config.py - https://dpaste.de/o8pS
test.py - https://dpaste.de/nGrF
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_marshmallow import Marshmallow app = Flask(__name__) app.config.from_object('api.config') db = SQLAlchemy(app) ma = Marshmallow(app)
– Leandro Campos