mod_python的类

mod_python的类

实在没辙了,还是拜托大家把

在mod_python中写的py文件怎么调用另一个文件中的函数或者类?

比如我有a.py和b.py
a.py

[Copy to clipboard] [ - ]
CODE:
class Aa:
    def __init__(self,string):
        self.str=string
    def getstr(self):
        return "<html><body>%s</body></html>"%self.str

def mm(string):
    return "<html><body>%s</body></html>"%string

b.py

[Copy to clipboard] [ - ]
CODE:
def test(req):
    import os
    import a
    c=a.Aa("ok")
    return c.getstr()

但是调用的时候 http://192.168.28.150/psp/lesson/b/test
总是发生错误:

[Copy to clipboard] [ - ]
CODE:
Mod_python error: "PythonHandler mod_python.publisher"

Traceback (most recent call last):

  File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
    result = object(req)

  File "/usr/lib/python2.3/site-packages/mod_python/publisher.py", line 136, in handler
    result = util.apply_fs_data(object, req.form, req=req)

  File "/usr/lib/python2.3/site-packages/mod_python/util.py", line 361, in apply_fs_data
    return object(**args)

  File "/data/wwwftp/psp/lesson/b.py", line 7, in test
    c=a.Aa("ok")

AttributeError: 'module' object has no attribute 'Aa'

可是在命令行上运行就没有问题
哪位能帮忙看一下到底哪里出了问题?
参考这个 http://www.dscpl.com.au/articles/modpython-002.html

一种方法是通过PythonPath添加要import的模块所在的目录

[Copy to clipboard] [ - ]
CODE:
<Directory /some/path>
    PythonPath "['/some/path/modules']+sys.path"
    AddHandler python-program .py
    PythonHandler myhandler
</Directory>

还可以使用mod_python的import_module()函数

[Copy to clipboard] [ - ]
CODE:
from mod_python import apache
a = apache.import_module("a",path=['/some/path/to/a])

哇赛,正点!
wolfg gg太感谢您了!