CherryPy配置tools.staticdir.root问题 [英] CherryPy configuration tools.staticdir.root problem
问题描述
如何使我的静态文件根目录相对于我的应用程序根文件夹(而不是硬编码路径)?
How can I make my static-file root directories relative to my application root folder (instead of a hard-coded path)?
根据CP指令 http://www.cherrypy.org/wiki/StaticContent )我已尝试以下操作我的配置文件:
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
但是当我运行 cherrpy.quickstart(rootclass, script_name ='/',config = config_file)
我得到以下错误
but when I run cherrpy.quickstart(rootclass, script_name='/', config=config_file)
I get the following error
builtins.ValueError:配置错误在部分:'全局',选项:'tree.cpapp',值:'cherrypy.Application(cpapp.Root())'。配置值必须是有效的Python。,'TypeError'解决名称'cpapp')))
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'",))
我知道我可以在quick.py之前调用main.py文件使用os.path.abspath(os.path.dirname(文件))),但我更喜欢使用单独的配置文件的想法,如果可能的话。
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.
任何帮助都将受到赞赏(如果相关,我使用CP 3.1与Python 3.1)
Any help would be appreciated (in case it is relevant, I am using CP 3.2 with Python 3.1)
TIA
Alan
TIA Alan
推荐答案
当您在配置项中引用模块时,CherryPy首先在 sys.modules
。所以一个解决方案是在你调用quickstart之前 import cpapp
。
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.
但是如果查找 sys.modules
失败,CherryPy尝试 __ import __
模块。由于这也是失败,您可能需要调查您的 cpapp.py
模块是否确实可以导入。
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.
请参阅 lib / reprconf.py
模块了解所有的详细信息。
See the lib/reprconf.py
module for all the gory details.
这篇关于CherryPy配置tools.staticdir.root问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!