How to import Flask into appengine - python

Getting the lovely error:
ERROR 2015-09-23 13:14:12,500 cgi.py:122] Traceback (most recent call last):
File "public/run.py", line 2, in <module>
from src import app
File "public/src/__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
I've installed flask into public/lib with pip install -t lib -r requirements.txt.
public/appengine_config.py
from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder
vendor.add('lib')
# also does not work
# import os
# vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
public/app.yaml
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /static
static_dir: src/static
- url: /
script: run.py
public/run.py
from google.appengine.ext.webapp.util import run_wsgi_app
from src import app
run_wsgi_app(app)
public/src/__init__.py
from flask import Flask
# import settings
app = Flask('app')
# app.config.from_object('src.settings')
import views

Take a look at the Starter Project. More importantly, you should change your app.yaml to point to the the flask wsgi app.
- url: .*
script: src.app
The run.py script and run_wsgi_app() is the old way to run your app and should not be used.

You might also want to check out the gae-init project as it uses Flask and is built on GAE. I found it's a great way to get up and running quickly.

Related

from app import app ImportError: cannot import name 'app'

flask-mega-tutorial and I found some error when I execute my code.
first I have init.py
from flask import Flask
app = Flask(__name__)
from app import routes
then routes.py
from app import app
#app.route('/')
#app.route('/index')
def index():
return "Hello, World!"
And last I have microblog.py to execute my code
from app import app
app.run(debug=True)
but when I execute FLASK_APP = microblog.py then flask run, I found this
Serving Flask app "microblog.py"
Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
Debug mode: off Usage: flask run [OPTIONS]
Error: While importing "microblog", an ImportError was raised:
Traceback (most recent call last): File
"e:\skripsiku\flask\lib\site-packages\flask\cli.py", line 235, in
locate_app import(module_name) File "E:\skripsiku\microblog.py",
line 1, in from app import app ImportError: cannot import
name 'app'
Oh and lastly there is my folder structure. I have main folder named skripsiku then in there I have app folder (in : init.py, routes.py) and microblog.py. Can anyone help me? thank you
try this:
from app import app as appl
you can use any name in place of appl
The directory for that tutorial is
minus the pycache and .vscode.
According to the tutorial, FLASK_APP=fua.py or microblog.py in your case.
I am assuming that you did flask run in the C:\xampp\htdocs\FUA App\app> directory since I faced a similar error when running that command from that directory.
I solved the problem by running the command from the C:\xampp\htdocs\FUA App.

Flask - Unable to run tests from tests folder (cannot perform relative import)

Hi i have the following project structure
project
app/
init.py
views.py
forms.py
models.py
static/
templates/
config.py
project_database.db
requirements.txt
run.py
tests/
test_project.py
run.py
from app import app
if __name__ == '__main__':
app.run()
app/init.py
from flask import Flask
from playhouse.flask_utils import FlaskDB
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config.DevelopmentConfig')
flask_db = FlaskDB(app)
database = flask_db.database
from app import views
tests/test_project.py
from ..app import app
import unittest
class TestProject(unittest.TestCase):
''' tests'''
When i try to run my tests i get the following error
Traceback (most recent call last): File "tests/test_status_code.py", line 1, in <module>
from ..app import app SystemError: Parent module '' not loaded, cannot perform relative import
If i move the tests file out of the folder it runs but i'd like to have it in the folder so I can have all test files in there.
I am using python 3.5 anf Flask 0.12.2
Thanks
Managed to get this working by doing the following
import sys
sys.path.append('../')
from app import app
import unittest
Class TestProject(unittest.TestCase):
But just cause this works is this right or is there a better more pythonic way?

DJANGO_SETTINGS_MODULE undefined

I know that there a lot of questions regarding this and I have actually gone through them all but I am unable to solve the problem. I am trying to run a python webapp on (python 2.7) GAE by following a tutorial for GAE based on python 2.5 (from: Head First Python chap. 10). This requires the creation of a form using djangoforms. However, on runnning GAE server, I got the error: "ImportError: no module named django.core.exceptions".
After following the various discussions regarding this error on this forum, I came to know that I had to add the following two lines to my app.yaml file for python 2.7:
libraries
- name: django
version: "1.4"
and had to include the 'threadsafe' attribute too. I even had to 'rename' my app name with an extension of '.app' instead of '.py' so that I am able to use the 'threadsafe' attribute.
However, I get the error 'DJANGO_SETTINGS_MODULE is undefined'. I tried to install django 'exclusively'. I even (desperately) created a new django project and moved the settings file into a subfolder titled 'django' in my webapp. However, I am still getting errors. Currently, my app.yaml file looks like this:
application: hfwwg
version: 1
runtime: python27
api_version: 1
threadsafe: true
env_variables:
DJANGO_SETTINGS_MODULE: 'django/settings.py'
handlers:
- url: /.*
script: hfwwg.app
- url: /static
static_dir: static
libraries:
- name: django
version: "1.4"
Please suggest how to remove the 'DJANGO_SETTINGS_MODULE' error or any other place where there is error. Any help would be appreciated.
And here is the portion of the code where I have used django:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms
...
class MyForm(djangoforms.ModelForm):
...
DJANGO_SETTINGS_MODULE: 'django/settings.py'
this should be
DJANGO_SETTINGS_MODULE: 'django.settings'
LINK

How to do relative imports on app engine? (python)

I'm trying to do a relative import in App Engine using python. Here is my basic situation:
app/
models.py
app.yaml
/mymodule/
test.py
mymodule.yaml
I'm trying to import models.py.. basically I have the same datastore models that are being using across different modules, so I was hoping to be able to import models.py from within test.py (or any other module).
How do relative imports work with App Engine? Thanks.
Edit: My app.yaml file:
application: [my app name]
version: main
runtime: python27
api_version: 1
threadsafe: true
inbound_services:
- mail
builtins:
- appstats: on
handlers:
[my handlers]
libraries:
- name: webapp2
version: "2.5.1"
- name: jinja2
version: latest
- name: markupsafe
version: latest
- name: ssl
version: latest
I believe the solution is to put the mymodule.yaml under app. It seems that the root of your code in the PYTHONPATH is the directory where your module's yaml file is.
Create one empty file under app called __init__.py to transform your directory into a package and then you will be able to import like:
from app import models
And you might want to add the same file into your mymodule directory as well.

connect to google datastore from Python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PyDev project for Google App Engine not finding webapp2
I am doing the helloworld tutorial and cannot connect from Python;
This is the app.yaml file:
application: "ceemee11111"
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.app
builtins:
- remote_api: on
This is start of helloworld.py:
import cgi
import datetime
import urllib
import webapp2
from google.appengine.ext import db
from google.appengine.api import users
class Greeting(db.Model):
"""Models an individual Guestbook entry with an author, content, and date."""
author = db.StringProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
in Python shell I chdir to helloworld
import helloworld
and get import error "No module named webapp2"
The app runs in localhost and also in ceemee11111.appspot.com without error.
As a test I commented out "import webapp2" and tried again and got an error
"No module named google.appengine.ext"
Please some ideas
Dan
applicaton
It seems that python can't find the library, so
Have you webapp2 installed in the system?. You can try with "pip install webapp2" or "easy_install"
If they are still missing, maybe you should check your "PYTHONPATH" variable, to check where is python looking for the libraries, and add yours to the path.

Categories