Matty
Matty

Reputation: 1981

Google App Engine - Importing my own source modules (multiple files)

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

Upvotes: 3

Views: 5400

Answers (4)

Pixelgrey
Pixelgrey

Reputation: 55

as @toby said, it must be imported as if importing from the top directory, and a file named init.py must be placed in the folder.

Upvotes: 1

Nick Johnson
Nick Johnson

Reputation: 101139

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.

Upvotes: 1

Evan Fosmark
Evan Fosmark

Reputation: 101651

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")

Upvotes: 2

airportyh
airportyh

Reputation: 22668

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

import modules.b

Upvotes: 8

Related Questions