views:

413

answers:

1

I have gdata library install on my ArchLinux, and a simple application which imports atom library at the beginning, when I run gapp engine and access that web app,

$ python2.5 ./dev_appserver.py ~/myapp

It throws exception 'No module named atom'. But when I run 'import atom' in Python2.5 interactive mode, it works well. How can I import atom module in my GAppEngine applications?

+8  A: 

Add atom.py to the same directory you keep you GAE Python sources in, and make sure it's uploaded to the server when you upload your app. (The upload happens when you do appcfg.py update myapp/ unless you go out of your way to stop it; use the --verbose flag on the command to see exactly what's being uploaded or updated).

(Or, if it's a large file, make a zipfile with it and in your handler append that zipfile to sys.path; see zipimport for example).

This assumes that you have a single file atom.py which is what you're importing; if that file in turns imports others you'll have to make those others available too in similar ways, and so on (see modulefinder in Python's standard library for ways to find all modules you need).

If atom is not a module but a package, then what you get on import is the __init__.py file in the directory that's the package; so the same advice applies (and zipimport becomes much more attractive since you can easily package up any directory structure e.g. with a zip -r command from the Linux command line).

If at any point (as modulefinder will help you discover) there is a dependency on a third party C-coded extension (a .so or .pyd file that Python can use but is not written in pure Python) that is not in the short list supplied with GAE (see here), then that Python code is not usable on GAE, as GAE supports only pure-Python. If this is the case then you must look for alternatives that are supported on GAE, i.e. pure-Python ways to obtain the same functionality you require.

Alex Martelli
This raises an interesting quandary. I'm as confident this answer is correct as I would be if I read the same thing on Google's website. If I'd done the latter, and someone answered with a paraphrase of the "official" answer, I'd upvote. But it seems a bit odd to upvote based on "I trust that Mr. Martelli knows whereof he speaks". +1 anyway, to get the ball rolling ;-)
Steve Jessop
"The official answer" is not in one place (zipimport is documented in two places I'm pointing to, third party libraries in one, modulefinder not in GAE docs but in Python docs [and again I give a URL], appcfg.py in yet another, ...) so what I'm doing is synthesizing all needed info in one place, from otherwise-fragmented locations, which seems more useful than just "a paraphrase";-). Anyway, all of this info is easy to experiment with and verify!-) But thanks for the trust anyway;-).
Alex Martelli
Sorry, yes, I didn't mean to imply that there exists a single "official" answer which would be better than this. Just that we can be reasonably confident that if there was one, this is roughly what it would say. So I was a bit surprised the answer went 20 minutes without an upvote ;-)
Steve Jessop