免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3065 | 回复: 2
打印 上一主题 下一主题

mod_python的类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-18 12:49 |只看该作者 |倒序浏览
实在没辙了,还是拜托大家把

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

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

  1. class Aa:
  2.     def __init__(self,string):
  3.         self.str=string
  4.     def getstr(self):
  5.         return "<html><body>%s</body></html>"%self.str

  6. def mm(string):
  7.     return "<html><body>%s</body></html>"%string
复制代码

b.py

  1. def test(req):
  2.     import os
  3.     import a
  4.     c=a.Aa("ok")
  5.     return c.getstr()
复制代码


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

  1. Mod_python error: "PythonHandler mod_python.publisher"

  2. Traceback (most recent call last):

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

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

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

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

  11. AttributeError: 'module' object has no attribute 'Aa'
复制代码


可是在命令行上运行就没有问题
哪位能帮忙看一下到底哪里出了问题?

论坛徽章:
0
2 [报告]
发表于 2005-12-18 20:55 |只看该作者
参考这个 http://www.dscpl.com.au/articles/modpython-002.html

一种方法是通过PythonPath添加要import的模块所在的目录
  1. <Directory /some/path>
  2.     PythonPath "['/some/path/modules']+sys.path"
  3.     AddHandler python-program .py
  4.     PythonHandler myhandler
  5. </Directory>
复制代码


还可以使用mod_python的import_module()函数
  1. from mod_python import apache
  2. a = apache.import_module("a",path=['/some/path/to/a])
复制代码

论坛徽章:
0
3 [报告]
发表于 2005-12-19 08:24 |只看该作者
哇赛,正点!
wolfg gg太感谢您了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP