How to import Flask app on test file to make unittest - python-3.x

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

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

Related

Python Error in __init__.py import modules

1. What I'm trying to do
I searched/read several posts on this forum, but no solution worked for me. This minor error is still holding me back.
I am trying the access some classes from a parent file and keep getting an ImportError.
2. My Setup
/website
__init__.py => from .client import Client
main.py => from client import Client
/client
__init__.py => from client import Client
client.py => from systeminfo import SystemInfo
systeminfo.py
The class I am trying to access in client.py is named Client.
3. The Error
Traceback (most recent call last):
File "..website\main.py", line 2, in
from client import Client
File "..website\client__init__.py", line 1, in
from client import Client
ImportError: cannot import name 'Client' from 'client' (..website\client__init__.py)
Can anyone have a look?
In your main.py file you're importing the method Client from the directory client. What you want to do is import the method Client from the file client in the directory client.
from client.client import Client

Can't run python app on digitalocean after adiing swagger

I have added flask_swagger_ui in my app. In Pycharm everything works good.
After uploading this code on digitalocean i have problem. I've also installed flask-swagger-ui on server and in uwsgi log have this:
File "/var/www/html/control/app.py", line 9
<<<<<<< HEAD
^
SyntaxError: invalid syntax
unable to load app 0 (mountpoint='') (callable not found or import error)
in app.py:
import os
from flask import Flask
from flask_restful import Api
from flask_jwt import JWT
from security import authenticate, identity
from resources.user import UserRegister
from flask_swagger_ui import get_swaggerui_blueprint #this is line 9

flask error: from app import views ImportError: cannot import name 'views'

So I am just starting a project in flask and I am using the proper folder structure for the first time and I keep getting this error. I am confused as to why and haven't faced this problem before.
I have tried renaming files and using from . import views but to no avail.
app.py
from flask import Flask
from app import views
app = Flask(__name__)
views.py
from app import app
#app.route('/')
def index():
return "Hello World!"
run.py
from app import app
app.run(debug=True)
Folder structure:
app
static
templates
app.py
run.py
views.py
tmp
Traceback (most recent call last):
File "app.py", line 2, in
from app import views
File "/Users/Josh/PycharmProjects/web1/app/app.py", line 2, in
from app import views
ImportError: cannot import name 'views'
app.py
from flask import Flask
app = Flask(__name__)
import views
views.py
from app import app
#app.route('/')
def hello():
return "Hello World!"
run.py
from app import app
app.run(debug=True)

Import issues Python3: import from package for flask project with api modules

I have my code structured as follows:
src/
--- api/
--- --- __init__.py
--- example_app.py
the init.py contains the following code:
from flask_restplus import Api
from api.about_api import api as about_api
from api.types_api import api as types_api
stackl_api = Api(<Snip>)
stackl_api.add_namespace(about_api)
stackl_api.add_namespace(types_api)
In example_app.py, I try to do this:
import stackl_api
app = Flask(__name__)
blueprint = Blueprint('stackl_api', __name__)
stackl_api.init_app(blueprint)
app.register_blueprint(blueprint)
But this gives the error
from .api import api │
ImportError: attempted relative import with no known parent package
if doing "from api import api" it gives "ModuleNotFoundError: No module named 'api' "
I'm probably forgetting something. Can you help?
In example_app.py, you need to replace
from api import api
By
import api

How do you upload your Web app to a production server?

I used this tutorial to get Flask running on an IIS server, and the test app works great.
Now, I have developed a web app in Visual Studios 2013 and it works great on the localhost dev server.
When I transfer the files to the server I get the following errors for every package but Flask even though, I pip installed all the required packages on the server.. Note, I did change init.py to app.py to coincide with the tutorial.
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "D:\...\wfastcgi.py", line 779, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\...\wfastcgi.py", line 621, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv('WSGI_HANDLER'))
File "D:\...\wfastcgi.py", line 605, in get_wsgi_handler
raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "app.app" could not be imported: Traceback (most recent call last):
File "D:\...\wfastcgi.py", line 589, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File ".\app.py", line 6, in <module>
from flask_bootstrap import Bootstrap
ImportError: No module named 'flask_bootstrap'
StdOut:
StdErr:
This error occurs with all the imports (except Flask), including flask_sqlalchemy, etc...
app.py (Using Python 3.5)
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_scss import Scss
from config import DevConfig, BaseConfig
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
app = Flask(__name__)
app.config.from_object(BaseConfig)
db = SQLAlchemy(app)
mail = Mail(app)
Bootstrap(app)
admin = Admin(app, name='Admin', template_mode='bootstrap3')
import AppName.views
My server folders are as such:
/roothostingfolder
app.py
config.py
models.py
views.py
wfastcgi.py
web.config
/static
style.css
/templates
layout.html
index.html
login.html
...
Do you know how to fix the error?

Resources