How to do relative imports on app engine? (python) - 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.

Related

Can't import Datastore from google.cloud

I'm trying to create a basic web app using Flask and Google Datastore to make myself to Google Cloud. Though, when I deploy my app, I get an Error 500, the details being that Python can't import Datastore : ImportError: No module named cloud.
Here is my app.yaml:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: .*
script: main.app
libraries:
- name: jinja2
version: "2.6"
- name: markupsafe
version: "0.15"
- name: flask
version: 0.12
My main.py starts like follows:
from __future__ import absolute_import
# Standard imports
import time
import logging
import json
import threading
# Flask framework
from flask import request
from flask import Flask
# Google Cloud features
from google.cloud import datastore
# the following replaces requests
from google.appengine.api import urlfetch
Finally, my requirements.txt is the following:
Flask
google-cloud
click
When I deploy my app (using gcloud app deploy) and get to my site, I get my error 500.
I don't understand why I can't use from google.cloud import datastore since it's what Google does in their tutorial... I must be missing something but I can't find what.
Any help would be appreciated.
From Installing the client library:
pip install --upgrade google-cloud-datastore
The client library you need (to match your import statement) is google-cloud-datastore, but you don't have that listed in your requirements.txt file.
Side note: your app.yaml file indicates your app is a standard env GAE app one, for which you have the optimized Google Datastore NDB Client Library library available, already included in your SDK (if you don't have specific reasons for choosing the generic one instead).
You do not specify env: flex so you are using Standard Environment. Here it is described for flex yaml and here it doesn’t appear for standard yaml.
This is important because google-cloud is not supported for Standard Environment. google-cloud’s repository:
These libraries currently do not run on Google App Engine Standard
Link mentioned by Dan Cornilescu about the NDB Client is an oficial solution for standard environment and few examples can be found in the oficial documentation:
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/ndb and https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/multitenancy

How to import Flask into appengine

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.

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

GAE - Flask can't import Jinja2 from subdirectory?

I'm working on a Google App Engine project using Flask. Flask is then using Jinja2. When I put Flask and all its required modules into the root folder of my project, the server started up just fine. However, I wanted to clean up the directory a bit so I moved Flask and other modules (including Jinja2) to a subdirectory called 'lib'. So my project looks like:
app.yaml
main.py
myapp
__init__.py
view.py
blahblah.py
lib
flask
jinja2
OtherModules
Then in the main.py file of the app I add the directory using sys.path.insert(0, 'lib'). Flask seems to import fine using this method, but Flask does not seem to be able to find Jinja2 with them both in the lib folder. When attempting to access a view on my running dev_appserver test I get:
File "lib/flask/__init__.py", line 19, in <module>
from jinja2 import Markup, escape
ImportError: No module named jinja2
How can I allow Flask to find Jinja2 (and allow other modules to find their requirements) while keeping them in the lib directory and not having to edit the modules to adjust paths?
Below is my main.py file in case something in there would be useful to know:
import sys
sys.path.insert(0, 'lib')
from google.appengine.ext.webapp.util import run_wsgi_app
from myapp import app
run_wsgi_app(app)
In order to include Jinja in you app engine application in your app.yaml file add these lines
libraries:
- name: jinja2
version: latest
Documentation for including more of the available libraries.
In order to use it for your local server you should install these also to your system. In a unix like system it would be
sudo easy_install jinja2
Additional information: there is an open source framework called gae-init, which combines your tech stack and provides a series of automations and good practices for app engine web services. Maybe worth having a look at it.

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