CherryPy配置tools.staticdir.root问题

发布于 2024-09-03 10:43:56 字数 775 浏览 3 评论 0原文

如何使静态文件根目录相对于应用程序根文件夹(而不是硬编码路径)?

根据 CP 说明 (http://www.cherrypy.org/wiki/StaticContent)我在配置文件中尝试了以下操作:

tree.cpapp = cherrypy.Application(cpapp.Root())
tools.staticdir.root = cpapp.current_dir

但是当我运行 cherrpy.quickstart(rootclass, script_name='/', config=config_file) 我收到以下错误

builtins.ValueError:(“部分中的配置错误:'global',选项:'tree.cpapp',值:'cherrypy.Application(cpapp.Root())'。配置值必须是有效的 Python。", 'TypeError', ("unrepr 无法解析名称 'cpapp'",))

我知道我可以在调用快速启动之前从 main.py 文件中进行配置(例如使用os.path.abspath(os.path.dirname(file))),但如果可能的话,我更喜欢使用单独的配置文件的想法。

任何帮助将不胜感激(如果相关,我正在使用 CP 3.2 和 Python 3.1)

TIA 艾伦

How can I make my static-file root directories relative to my application root folder (instead of a hard-coded path)?

In accordance with CP instructions (http://www.cherrypy.org/wiki/StaticContent) I have tried the following in my configuration file:

tree.cpapp = cherrypy.Application(cpapp.Root())
tools.staticdir.root = cpapp.current_dir

but when I run cherrpy.quickstart(rootclass, script_name='/', config=config_file)
I get the following error

builtins.ValueError: ("Config error in section: 'global', option: 'tree.cpapp', value: 'cherrypy.Application(cpapp.Root())'. Config values must be valid Python.", 'TypeError', ("unrepr could not resolve the name 'cpapp'",))

I know I can do configuration from within the main.py file just before quickstart is called (eg. using os.path.abspath(os.path.dirname(file))), but I prefer using the idea of a separate configuration file if possible.

Any help would be appreciated (in case it is relevant, I am using CP 3.2 with Python 3.1)

TIA
Alan

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

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

发布评论

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

评论(1

素罗衫 2024-09-10 10:43:56

当您引用配置条目中的模块时,CherryPy 首先在 sys.modules 中查找该模块。因此,一种解决方案是在调用快速启动之前import cpapp

但是,如果 sys.modules 中的查找失败,CherryPy 会尝试 __import__ 该模块。由于这也失败了,您可能需要调查您的 cpapp.py 模块是否确实可导入。

有关所有详细信息,请参阅 lib/reprconf.py 模块。

When you refer to a module inside configuration entries, CherryPy first looks for that module in sys.modules. So one solution would be to import cpapp just before you call quickstart.

But if that lookup in sys.modules fails, CherryPy tries to __import__ the module. Since that is also failing, you might need to investigate whether your cpapp.py module is indeed importable at all.

See the lib/reprconf.py module for all the gory details.

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