如何在gae-python上导入父模型

发布于 2024-09-05 04:49:46 字数 555 浏览 2 评论 0原文

main:.
├─a
│     ├─__init__.py
│     └─aa.py
├─b
│     ├─__init__.py
│     └─bb.py
└─cc.py

如果我在 aa.py 中,如何导入 cc.py ?

这是我的代码,但它是错误的:

from main import cc

我应该做什么。

感谢

更新

在正常的 python 文件(不在 gae 上)中

import os,sys
dirname=os.path.dirname
path=os.path.join(dirname(dirname(__file__)))
sys.path.insert(0,path)
import cc
print cc.c

,我可以使用此代码:但在 gae 上,它显示错误:

ImportError: No module named cc
main:.
├─a
│     ├─__init__.py
│     └─aa.py
├─b
│     ├─__init__.py
│     └─bb.py
└─cc.py

if i am in aa.py , how to import cc.py ?

this is my code ,but it is error :

from main import cc

what should i do .

thanks

updated

in normal python file (not on gae),i can use this code :

import os,sys
dirname=os.path.dirname
path=os.path.join(dirname(dirname(__file__)))
sys.path.insert(0,path)
import cc
print cc.c

but on gae , it show error :

ImportError: No module named cc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫相离 2024-09-12 04:49:46

我不明白您所显示的代码如何可能对您来说失败。为了重现您的问题,我构建了以下精简项目:

$ ls -lR
total 32
-rw-r--r--  1 aleax  staff    0 Jun 10 21:20 __init__.py
drwxr-xr-x  4 aleax  staff  136 Jun 10 21:28 a
-rw-r-----@ 1 aleax  staff  107 Jun 10 21:27 app.yaml
-rw-r--r--  1 aleax  staff   21 Jun 10 21:20 cc.py
-rw-r--r--  1 aleax  staff  471 Jun 10 21:25 index.yaml
-rw-r--r--  1 aleax  staff   75 Jun 10 21:20 main.py

./a:
total 8
-rw-r--r--  1 aleax  staff    0 Jun 10 21:20 __init__.py
-rw-r--r--  1 aleax  staff  130 Jun 10 21:20 aa.py

这是非空的 Python 文件:

$ for f in main.py cc.py a/aa.py; do echo "File: $f"; cat $f; echo; done
File: main.py
print 'Content-Type: text/plain'
print ''
print 'in main'
from a import aa

File: cc.py
print 'in cc'
c = 23

File: a/aa.py
import os, sys
dirname=os.path.dirname
path=os.path.join(dirname(dirname(__file__)))
sys.path.insert(0,path)
import cc
print cc.c
$ 

正如预测的那样,它运行得很好,显示(无论是在 SDK 上本地运行时还是在 google 服务器上运行时) appspot.com):

in main
in cc
23

因此您的代码部分中一定存在一些您没有向我们展示的其他错误。请通过复制这个小项目并在本地和 appspot.com 上尝试来确认这一点,并让我们知道它如何为您工作(或失败......?)。

I don't understand how the code you've shown can possibly be failing for you. Trying to reproduce your problem, I built the following pared-to-the-bone project:

$ ls -lR
total 32
-rw-r--r--  1 aleax  staff    0 Jun 10 21:20 __init__.py
drwxr-xr-x  4 aleax  staff  136 Jun 10 21:28 a
-rw-r-----@ 1 aleax  staff  107 Jun 10 21:27 app.yaml
-rw-r--r--  1 aleax  staff   21 Jun 10 21:20 cc.py
-rw-r--r--  1 aleax  staff  471 Jun 10 21:25 index.yaml
-rw-r--r--  1 aleax  staff   75 Jun 10 21:20 main.py

./a:
total 8
-rw-r--r--  1 aleax  staff    0 Jun 10 21:20 __init__.py
-rw-r--r--  1 aleax  staff  130 Jun 10 21:20 aa.py

Here are the nonempty Python files:

$ for f in main.py cc.py a/aa.py; do echo "File: $f"; cat $f; echo; done
File: main.py
print 'Content-Type: text/plain'
print ''
print 'in main'
from a import aa

File: cc.py
print 'in cc'
c = 23

File: a/aa.py
import os, sys
dirname=os.path.dirname
path=os.path.join(dirname(dirname(__file__)))
sys.path.insert(0,path)
import cc
print cc.c
$ 

It runs just fine, as predicted, showing (both when run locally on the SDK and when run on google's servers at appspot.com):

in main
in cc
23

So there must be some other error in parts of your code that you're not showing us. Please confirm this by reproducing this tiny project and trying it both locally and on appspot.com and let us know how it works (or fails...?) for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文