+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
Coding Thread, Python/GAE import modules issue? in Coding and Web Development; I'm fiddling with Python and Google Apps Engine (using the API, installed on Windows 7) to get my head round ...
  1. #1

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925

    Python/GAE import modules issue?

    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

    Code:
    def outputText():
        return "hello world, again"
    Then, my main.py is as follows:

    Code:
    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:

    Code:
    --> --> -->
    
    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 <module>    import displayMe.outputTextImportError: No module named displayMe.outputText
    What am I doing wrong?!

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    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
    Last edited by Steve21; 25th August 2011 at 02:11 PM. Reason: Typo

  3. #3

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925
    Quote Originally Posted by Steve21 View Post
    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'.

  4. #4

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    Where is the module? I'm assuming it's in the correct filepath "from python" not the file?

    Steve

  5. #5

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925
    It is in the same directory as the main file (ie. main.py and displayMe.py are in the same directory).

  6. #6

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    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
    Last edited by Steve21; 25th August 2011 at 02:33 PM.

  7. #7

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925
    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.

  8. #8

    Join Date
    Jul 2005
    Location
    Rugby
    Posts
    432
    Thank Post
    17
    Thanked 66 Times in 61 Posts
    Rep Power
    36
    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

  9. #9

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925
    Quote Originally Posted by saundersmatt View Post
    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.

  10. #10

    Join Date
    Jul 2005
    Location
    Rugby
    Posts
    432
    Thank Post
    17
    Thanked 66 Times in 61 Posts
    Rep Power
    36
    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

  11. #11

    Join Date
    Jul 2005
    Location
    Rugby
    Posts
    432
    Thank Post
    17
    Thanked 66 Times in 61 Posts
    Rep Power
    36
    Hi,

    I've just duplicated your code,

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

    The relevant code sections are:
    Import;
    Code:
    from displayMe import outputText
    Call;
    Code:
    self.response.out.write(outputText())
    Equally the following would work
    Code:
    import displayMe
    self.response.out.write(displayMe.outputText())

    Matt

  12. #12

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925
    Must be something wrong with my install then, as neither of those work.

  13. #13

    Join Date
    Jul 2005
    Location
    Rugby
    Posts
    432
    Thank Post
    17
    Thanked 66 Times in 61 Posts
    Rep Power
    36
    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

  14. #14

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925
    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.

  15. #15

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    18,529
    Thank Post
    527
    Thanked 2,648 Times in 2,049 Posts
    Blog Entries
    24
    Rep Power
    925
    Right, just tested as a new app, (attached).

    Same error.
    Attached Files Attached Files



SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Photoshop Elements Photo Browser issue.
    By mseaney in forum Educational Software
    Replies: 1
    Last Post: 17th January 2006, 01:02 PM
  2. Synchronise Files issue.....Mystery
    By tosca925 in forum Windows
    Replies: 3
    Last Post: 13th January 2006, 11:31 AM
  3. Linux Format, January Issue
    By kingswood in forum *nix
    Replies: 11
    Last Post: 5th December 2005, 12:02 PM
  4. Exchange 2003 and Server 2003 SP1 issue.
    By tosca925 in forum Windows
    Replies: 0
    Last Post: 21st August 2005, 11:32 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •