Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

I'm fiddling with Python and Google Apps Engine (using the API, installed on Windows 7) to get my head round it, and I've hit a wall.

 

I've created a very simple module 'displayMe.py' and in that file is a single function

 

def outputText():
   return "hello world, again"

 

Then, my main.py is as follows:

 

from google.appengine.ext import webappfrom google.appengine.ext.webapp import util


import displayMe.outputText


class MainHandler(webapp.RequestHandler):
   def get(self):
       self.response.out.write(displayMe.outputText())
       


def main():
   application = webapp.WSGIApplication([('/', MainHandler)],
                                        debug=True)
   util.run_wsgi_app(application)


if __name__ == '__main__':
   main()

 

So, I hope someone agrees - this isn't a complex application, and from everything I've read, this should just work! Instead I get a nice error and trace:

 

[color=#F0F0F8][font=Times New Roman]--> [/font][/color][size=-5][color=#f0f0f8][color=#F0F0F8][font=Times New Roman]--> -->[/font][/color][/color][/size]

Traceback (most recent call last):  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 4143, in _HandleRequest    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 4049, in _Dispatch    base_env_dict=env_dict)  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 616, in Dispatch    base_env_dict=base_env_dict)  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3120, in Dispatch    self._module_dict)  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3024, in ExecuteCGI    reset_modules = exec_script(handler_path, cgi_path, hook)  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2887, in ExecuteOrImportScript    exec module_code in script_module.__dict__  File "C:\Users\Tony\AppEngine Apps\testapp2\testapp\src\main.py", line 20, in     import displayMe.outputTextImportError: No module named displayMe.outputText

 

What am I doing wrong?!

Posted (edited)

You don't import a specific function, You either import the module aka import displayme, or import a single function aka from displayme import outputtext

 

Steve

Edited by Steve21
Typo
Posted
You don't import a specific function, You either import the module aka import displayme, or import a single function aka from displayme import outputtext

 

Steve

 

I've tried:

 

from displayMe import outputText

import displayMe

import displayMe.outputText

 

All of them result in errors.

 

The problem is not the function being imported as such, it is that it doesn't recognise that there is a module titled 'displayMe'.

Posted (edited)

And those are where? :D

 

My point being, paths are done from Python.exe, Not from the "main" file, or at least I'm sure they are from when I last used it

 

Try putting the module into the Python module folder, and try it, "and/or" use the full file path. As from how I understand you're effectively asking it to load it from the module folder, not "insert filepath"

 

Edit - Or try it like this:

 

import sys

sys.path.append('path/to/file')

import displayMe

 

Steve

Edited by Steve21
Posted

I'm using Google Apps Engine, so it has strict rules about where things have to be. So, I have an application defined within the Google App Launcher, and everything is within the location defined within the engine.

 

Altering anything to do with the python install is simply not possible.

 

So, all modules and things have to be within the application space.

 

From what I can tell, the directory with my main.py file in is included in the python path, but if I were to want to use any subdirectories, I'd have to programatically add them to the path via a small fix Google released.

Posted

Reading a few more bits "import displayMe" should work fine.

Perhaps "import testapp.src.displayme"

 

I'm trying to locate the source of the few GAE apps i've written to see, but i'm having issues with the sdk msi, bear with me.

 

Matt

Posted

Hi,

 

I've just duplicated your code,

 

I have displayMe.py in the same directory as main.py

 

The relevant code sections are:

Import;

from displayMe import outputText

Call;

self.response.out.write(outputText())

 

Equally the following would work

import displayMe
self.response.out.write(displayMe.outputText())

 

 

Matt

Posted

I did a clean install with the latest GAE sdk and latest python 2.7.*.

 

Do you want to swap zip files of the a testapp directories?

 

I have just noticed that you have a "src" folder did you create that yourself? Mine is just everything in one root directory.

 

Matt

 

Matt

Posted

You shouldn't be using 2.7 - GAE is 2.5 based, so things that happen in it don't happen in 2.7 etc...

 

I have a src folder because I'm using eclipse and the pydev plugin.

 

I'll try creating a fresh non-eclipse app and see if it happens there.

Posted

In main.py you have written:

import displayMe.outputText

This is incorrect usage of the python import statement, you either must import the entire module using "import displayMe" and then call functions with displayMe.outputText; or import functions with "from displayMe import outputText, *additional_function*" and call the functions by JUST function name.

 

Matt

Posted

OK. I now have it working. It appears that this is a bug with Python 2.5, which their site links to, instead of linking to the latest Python MSI in the 2.5 series - 2.5.4.

 

Remove 2.5 and put 2.5.4 on and it works fine.

Posted
In main.py you have written:

import displayMe.outputText

This is incorrect usage of the python import statement, you either must import the entire module using "import displayMe" and then call functions with displayMe.outputText; or import functions with "from displayMe import outputText, *additional_function*" and call the functions by JUST function name.

 

Matt

 

Not according to a bunch of sites I've read. However, the problem was, the from blah import blah method was failing too.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...