Question

I want to make a docstring for my router module in gae. I also know it must be the first thing in the module (after the file encoding type).

The thing is, if you run this module alone you get nothing but an import error (No module named webapp2). What I wanted is to print the docstring when running just the file but this import error just don't let me. Is there any way to do this?

I tried:

if __name__ == "__main__":
    print help(self)

And other combinations, but no success.

[EDIT]

No specific code. Could be appengine's example:

# coding: utf-8
""" docstring """

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)
Était-ce utile?

La solution

The ImportError happens when you run it as a standalone because it won't include any of the 'magic' that is included when run as an app. For instance, if you look at dev_appserver.py (just the basic one you use to run a dev server), you'll see this function:

def fix_sys_path(extra_extra_paths=()):
  """Fix the sys.path to include our extra paths."""
  extra_paths = EXTRA_PATHS[:]
  extra_paths.extend(extra_extra_paths)
  sys.path = extra_paths + sys.path

Here you can see that sys.path is being modified to include some 'extra' paths, and if we take a look at one of them, you'll see webapp2 (as well as additional libraries provided in the SDK):

EXTRA_PATHS = [
  # ...other similar setups...
  os.path.join(DIR_PATH, 'lib', 'webapp2'),
  # ...other similar setups...
]

You can see GAE is performing some additional steps behind the scenes to let you say import webapp2 without issue. Therefore when you try to run it on its own, you will get that error because your system is just checking the standard paths for webapp2 (which you likely don't have installed).

And that doesn't really answer your question at all :) As for that, I'm sure there are definitely more elegant/appropriate ways of handling this, but one thing you could try is wrapping your import(s) in a try/except block and on ImportError, checking if you're running the module directly. If so, call the module docstring and exit. Note this is just an example - you would want to make this more refined if you were to actually use it:

"""Module information."""
import sys

try:
    import webapp2
except ImportError:
    if __name__ == '__main__':
        print __doc__
    else:
        print 'Webapp2 not found'
    sys.exit(1)


class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

This will print Module information if you run it directly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top