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
Related
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
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
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
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?
I have a basic flask-restful application with a structure that looks like this as recommended on the flask website.
/application
/application
/config.py
/__init__.py
/wsgi.ini
Slightly irrelevant, but config.py is generated by our CD server.
wsgi.ini looks likes this
[uwsgi]
module = application:app
master = true
processes =5
socket = /tmp/application.sock
chmod-socket = 660
vacuum = true
die-on-term = true
and __init__.py looks something like this
import config
from flask import Flask, request, g
from flask_restful import Resource, Api, abort
app = Flask(__name__)
api = Api(app)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Now when I try to startup the application using uwsgi --ini wsgi.ini I get this error:
File "./application/__init__.py", line 2, in <module>
import config
Originally __init__.py was called main.py and I executed that to debug, which is why the import is now wrong. I'm guess I will need to change the import to be from .config import *
My question is two fold:
Could I have avoided the import problem completely. ie is there a way in python3 to import sibling modules that will work for both approaches
is my wsgi.ini in the correct place, or should it be in the inner application directory?
Python 3.x dropped support for infra-package relative imports. You need to use an absolute import:
from application import config
or the new spelling of relative imports:
from . import config
If you need to also support legacy versions of Python you can enable this behavior with:
from __future__ import absolute_import