0
votes

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

1
where is your flask app defined? i.e. this line of code: app = Flask(name)Nathan
On __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)Leandro Campos
I believe the issue is you have a file named app.py and you're creating the flask app within your init.py file as a variable also called app so python is having issues deciphering what app you want imported. Maybe you could rename app.py to something else? Then import app from test.py like so: from api import appNathan
Renamed app.py to wgsi.py and change the imports to from api import app, still the error: ModuleNotFoundError: No module named 'api'Leandro Campos
@Nathan i've posted the full code, i spent hours trying to make test works, i don't know what to do =/Leandro Campos

1 Answers

-1
votes

You need fix some imports for the code to run...

Example for import objects from init.py do you use

from . import app, db

or create a package named app and input init.py inside