localzuk Posted August 25, 2011 Posted August 25, 2011 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?!
Steve21 Posted August 25, 2011 Posted August 25, 2011 (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 August 25, 2011 by Steve21 Typo
localzuk Posted August 25, 2011 Author Posted August 25, 2011 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'.
Steve21 Posted August 25, 2011 Posted August 25, 2011 Where is the module? I'm assuming it's in the correct filepath "from python" not the file? Steve
localzuk Posted August 25, 2011 Author Posted August 25, 2011 It is in the same directory as the main file (ie. main.py and displayMe.py are in the same directory).
Steve21 Posted August 25, 2011 Posted August 25, 2011 (edited) And those are where? 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 August 25, 2011 by Steve21
localzuk Posted August 25, 2011 Author Posted August 25, 2011 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.
saundersmatt Posted August 25, 2011 Posted August 25, 2011 python - Google App Engine - Importing my own source modules (multiple files) - Stack Overflow the above suggests you should use the syntax import modules.displayMe and call it using displayMe.OutputText() Matt
localzuk Posted August 25, 2011 Author Posted August 25, 2011 python - Google App Engine - Importing my own source modules (multiple files) - Stack Overflow the above suggests you should use the syntax import modules.displayMe and call it using displayMe.OutputText() Matt There's no such folder as 'modules'. What would the top level be? And the 'sys' bit doesn't work - same error.
saundersmatt Posted August 25, 2011 Posted August 25, 2011 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
saundersmatt Posted August 25, 2011 Posted August 25, 2011 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
localzuk Posted August 25, 2011 Author Posted August 25, 2011 Must be something wrong with my install then, as neither of those work.
saundersmatt Posted August 25, 2011 Posted August 25, 2011 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
localzuk Posted August 25, 2011 Author Posted August 25, 2011 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.
localzuk Posted August 25, 2011 Author Posted August 25, 2011 Right, just tested as a new app, (attached). Same error.helloworld.zip
saundersmatt Posted August 25, 2011 Posted August 25, 2011 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
saundersmatt Posted August 25, 2011 Posted August 25, 2011 Attached is the sample app that I created, for reference. testapp.zip Matt
localzuk Posted August 25, 2011 Author Posted August 25, 2011 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.
localzuk Posted August 25, 2011 Author Posted August 25, 2011 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now