views:

919

answers:

3

Hello,

I am writing a GAE application and am having some difficulty with the following problem.

I've created multiple python files (say a.py and b.py) which are both stored in the same folder. I am able to call code in a.py or b.py by mapping URL's to them (using app.yaml). What I haven't figured out how to do is import the code from one into another.

Can anyone help me with the syntax and/or any config that is required here? For instance, I am under the impression that I can include the code from b.py in the file a.py by issuing the following statement in a.py

import b

I'm not having any success with this approach. Specifically I receive this error:

ImportError: No module named b

Any suggestions?

Thanks,

Matt

+6  A: 

Have you tried importing as if you were starting at the top level? Like

import modules.b
toby
thanks. that did the trick
Matty
+2  A: 

If the files a.py and b.py aren't located, be sure to include the respective paths in sys.path.

import sys
sys.path.append(r"/parent/of/module/b")
Evan Fosmark
+1  A: 

Note that the usual pattern with GAE is not to have each one independently mapped in app.yaml, but rather to have a single 'handler' script that has all (or all but static and special) URLs mapped to it, and have that script import both a and b and use Handlers they define.

Nick Johnson
Is that pattern a best-practice? It seems like that would make every request take the same amount of resources.
Thomas L Holaday
Yes, it is a best-practice: All the modules in your app are loaded into a single Python environment that gets reused for each request, so stuff stays in memory between requests regardless. The only additional overhead is loading all the modules on the first request.
Nick Johnson