WSGI 的本地导入路径对我不起作用
我编写了一个 WSGI 脚本,该脚本依赖于在同一目录中导入文件。 WSGI 使用路径的方式不同吗?难道我的(mod_wsgi apachae)服务器疯了?
I wrote a WSGI script that depends on importing files in the same directory. Does WSGI use paths differently? Could my (mod_wsgi apachae) server be insane?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WSGI 服务器的当前工作目录可以是任何目录,并且不同的 WSGI 服务器的当前工作目录可能不同。此外,命令行 Python 解释器默认情况下将查找当前工作目录,而对于嵌入式系统则不会。
最终结果是,依赖于从与 WSGI 脚本文件相同的目录导入模块是不可移植的。您需要根据所使用的 WSGI 服务器设置 sys.path 或 PYTHONPATH,以确保可以导入特定位置的模块。您不能像命令行 Python 那样依赖默认的当前工作目录查找。
The current working directory for a WSGI server could be anything and for different WSGI servers could be different. Further, command line Python interpreter will by default look in the current working directory where as for an embedded system it will not.
End result is that relying on being able to import modules from the same directory as a WSGI script file is not portable. You will need to set sys.path or PYTHONPATH as appropriate for the WSGI server being used to ensure that modules from a specific location can be imported. You cannot rely on a default of looking in the current working directory as may occur for command line Python.