I downloaded the experimental version of PyCrypto (pycrypto-2.7a1.tar.gz). I have copied the "Crypto" directory (extracted from pycrypto-2.7a1.tar.gz) to my project folder.
In app.yaml file:
libraries:
- name: pycrypto
version: 2.7 # latest
I get error (at the time of deployment) if I try to give version as 2.7a1 or 2.7 for PyCrypto in app.yaml:
appcfg.py: error: Error parsing C:\gaurav\coding\python\x\x\app.yaml: pycrypto version "2.7" is not supported, use one of: "2.3", "2.6" or "latest" ("latest" recommended for development only)
in "C:\gaurav\coding\python\x\x\app.yaml", line 73, column 1.
How do I provide the correct PyCrypto version in app.yaml ?
You use the app.yaml file to tell App Engine which libraries and versions you want to use only for those Third-party libraries available at the platform.
In your case, you want to use a version of the library that is not available, so you can't use that method to configure it.
Instead of that, you can upload to App Engine the libraries you want to use by following the method outlined in this other question:
To download the library and unzipped inside your GAE application directory. In this example, the destination directory is called pycrypto26.
To include the path to that library with something like
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pycrypto26/lib'))
To import the relevant modules
import Crypto
from Crypto.Hash import SHA256, SHA512
A full working example is
import webapp2
import logging
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pycrypto26/lib'))
import Crypto
from Crypto.Hash import SHA256, SHA512
class MainPage(webapp2.RequestHandler):
def get(self):
logging.info("Running PyCrypto with version %s" % Crypto.__version__)
self.response.write('<html><body>')
self.response.write( SHA256.new('abcd').hexdigest() + "<br>" )
self.response.write( SHA512.new('abcd').hexdigest() + "<br>")
self.response.write('</body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
Related
App runs fine with just the app.yaml, main.py and requiremets.txt (Flask)
I also have a mymodule.py file.
If, in main.py, I do
import mymodule
or
from mymodule import myfunc
I get the 502 Bad Gateway
runs fine locally though..
Please follow the official documentation on :
Specifying Private Dependencies
To use private dependencies:
1.Run pip install -t lib my_module to copy dependencies into a local
folder named lib.
2.Add an empty __init__.py file to the lib directory to make it a
module.
3.Import the module in your app. For example:
import lib.my_module
I am trying to import NLTK library in Google App Engine it gives error, I created another module "testx.py" and this module works without error but I dont know why NLTK does not work.
My code
nltk_test.py
import webapp2
import path_changer
import testx
import nltk
class MainPage(webapp2.RequestHandler):
def get(self):
#self.response.headers['Content-Type'] = 'text/plain'
self.response.write("TEST")
class nltkTestPage(webapp2.RequestHandler):
def get(self):
text = nltk.word_tokenize("And now for something completely different")
self.response.write(testx.test("Hellooooo"))
application = webapp2.WSGIApplication([
('/', MainPage), ('/nltk', nltkTestPage),
], debug=True)
testx.py code
def test(txt):
return len(txt)
path_changer.py code
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'nltk'))
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'new'))
app.yaml
application: nltkforappengine
version: 0-1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: nltk_test.application
- url: /nltk.*
script: nltk_test.application
libraries:
- name: numpy
version: "1.6.1"
This code works fine When I comment the import nltk and nltk related code, I think NLTK is not imported, please help me to sort out this problem, thanks
Where do you have nltk installed?
GAE libraries need to be available in your app folder. If you have nltk elsewhere in your pythonpath it won't work.
I want to have multiple pages in google-app engine (python 2.7) and the followoing is my directory structure:
root contains: pujaweb.py (the main script), index.html, stylesheets(folder), port(folder)...
now my second script for the page is in the port folder and i want to configure my index.html such that it links to that page. I have tried a lot of stuff but somehow it does not work and always shows 404 page not found error and the command line says that PujaPort module does not exist (pujaport being the app handler for the second page).
the following is my app.yaml file code:
application: thepujabhalerao
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /images
static_dir: images
- url: /stylesheets
static_dir: stylesheets
- url: /port
script: pujaport.app
- url: /.*
script: pujaweb.app
libraries:
- name: jinja2
version: latest
this is my main handler (pujaweb.py)
import cgi
import webapp2
import jinja2
import os
from google.appengine.api import xmpp
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template_values = {}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
and the href in the index filer looks like this:
P
and finally this is the second page handler pujaport.py:
import cgi
import webapp2
import jinja2
import os
from google.appengine.api import xmpp
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class PujaPort(webapp2.RequestHandler):
def get(self):
self.response.out.write("In handler")
app = webapp2.WSGIApplication([('/port', PujaPort)],
debug=True)
I know its a minor glitch somewhere but please help me out as after being on it for too long i maybe missing the obvious.
If you insist on using that folder structure, you'll need to make the port directory into a package by adding an __init__.py file to it (which can be empty), and reference the app as port.pujaport.app, its fully qualified name.
I'm using GAE-Sessions with Google App Engine. In the readme file it says "If you want to gae-sessions with Django, add 'gaesessions.DjangoSessionMiddleware' to your list of MIDDLEWARE_CLASSES in your settings.py file." Problem is I don't have a "settings.py" file, nor is one created when a App Engine project is created. What settings.py file is GAE-Sessions referring to?
I am using gae-sessions with google appengine django. What I have is a file in the same level as app.yaml which I call it appengine config.
The contents are
from gaesessions import SessionMiddleware
import logging
def webapp_add_wsgi_middleware(app):
app = SessionMiddleware(app, cookie_key="ExampleofarandomKEYforcookieswritewhatyouwant")
return app
In the same level I have also placed the gaesessions folder with the __init__.py file.
Download django-nonrel and use the django-admin.py helper to create all the boilerplate project, including settings.py file.
Documentation here.
I'm learning developing in Google App Engine.
This is one of the code from the tutorial, http://code.google.com/appengine/docs/python/gettingstarted/usingwebapp.html
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
I've almost identical code. I sometime get warning:
WARNING 2011-06-30 13:10:44,443 init.py:851] You are using the default Django version (0.96). The default Django version will change in an App Engine release in the near future. Please call use_library() to explicitly select a Django version. For more information see http://code.google.com/appengine/docs/python/tools/libraries.html#Django
Can anyone please re factor the above code with use_library(). I'm not sure how to start and where to use use_library and what to do with webapp.
Thanks in advance.
The above code should not require you to call use_library directly.
If you create a new file in the root directory of your application named appengine_config.py and add the following line to it:
# Make webapp.template use django 1.2
webapp_django_version = '1.2'
try putting this code on top of your module :
import os
from google.appengine.dist import use_library
use_library('django', '1.2')
In the current version this is even simpler as third-party libraries are now specified in app.yaml
libraries:
- name: django
version: "1.2"
You can also use webapp2 that includes Django’s templating engine.
import webapp2
from google.appengine.ext.webapp2 import template