且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何在web2py中使用模块?

更新时间:2023-02-21 16:57:52

func has to be in a place that is in PYTHONPATH (sys.path) such as web2py/site-packages. This is the proper Pythonic way of doing it.

If instead you want to package your module with your app, then place it in applications/yourapp/modules, next, you should import it with func = local_import('func')

Q: People who are new to web2py may wonder: why use local_import() when the former method is the standard one in the Python world?

A: Because in web2py you can install new apps without restarting the server. Apps cannot change sys.path because it is not thread safe and would depend on the order those apps are installed. Moreover two apps may require modules with the same name but different (for example different versions). We want to avoid conflicts between those modules and other modules that may be pre-installed.

local_import() is a workaround that allows you to ship some modules with your apps and import them without adding the folder to sys.path and causing version conflicts.

相关阅读

技术问答最新文章