[英]Why we need to specify absolute path in Bottle + uWSGI?
I'm developing a Bottle application. 我正在开发一个Bottle应用程序。 My program reads configurations from configuration (.cfg) files and also using template (.tpl) files.
我的程序从配置(.cfg)文件中读取配置,也使用模板(.tpl)文件。
But when I host my app on nginx using uWSGI, it cannot find the files (given relative paths to the project) 但是当我使用uWSGI在nginx上托管我的应用程序时,它无法找到文件(给定项目的相对路径)
What is the possible solution?? 什么是可能的解决方案?
Instead of using relative paths, store an absolute path based on the current module path: 不使用相对路径,而是根据当前模块路径存储绝对路径:
MODULEPATH = os.path.dirname(__file__)
template = open(os.path.join(MODULEPATH, 'templates/sometemplate.tpl').read()
__file__
is the filename of the current module, os.path.dirname(__file__)
is the directory the module resides in. __file__
是当前模块的文件名, os.path.dirname(__file__)
是模块所在的目录。
You should never rely on relative paths in Python code; 你永远不应该依赖Python代码中的相对路径; the current working directory is not changed when running a python program.
运行python程序时不更改当前工作目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.