Google App Engine - Importing my own source module

2019-07-06 05:29发布

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

4条回答
霸刀☆藐视天下
2楼-- · 2019-07-06 05:56

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")
查看更多
相关推荐>>
3楼-- · 2019-07-06 05:59

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

import modules.b
查看更多
4楼-- · 2019-07-06 06:07

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.

查看更多
该账号已被封号
5楼-- · 2019-07-06 06:15

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.

查看更多
登录 后发表回答