gpt4 book ai didi

python - web2py 中使用 import 进行函数调用

转载 作者:行者123 更新时间:2023-12-01 06:09:39 24 4
gpt4 key购买 nike

我已将代码拆分为多个文件。我已将所有其他文件中的所有函数导入到 admin.py 中。假设我想调用一个函数 XYZ。如果我将函数路径指定为 admin/XYZ ,则会给出无效函数的错误,为此我必须将路径指定为 file_with_XYZ_function/XYZ

有没有一种方法可以克服这个问题,并简单地从一个文件中调用所有导入的函数

最佳答案

注意:这可能无法回答您的问题,因为我不确定我是否理解您的问题...

但是,如果您想将一些代码放入(共享)模块中并从多个 Controller 中包含它,我建议您查看 web2py book 的第四章(核心)和搜索local_import

For this kind of situation, web2py provides another way to import modules in such a way that the global sys.path is not altered: by placing them in the "modules" folder of an application. One side benefit is that the module will be automatically copied and distributed with the application; however, there are certain restrictions that apply. web2py provides a local_import function that must be used to import modules from the "modules" folder.

模块的导入取决于模块以及 web2py 可以在哪里找到它们。如果它是 web2py 可以在 sys.path 或 web2py/site-packages 中找到的标准模块 import modulename 应该按预期工作。

对于应用程序本地的模块,web2py 提供了其他功能:applications/appname/modules

可以使用 local_import 导入这些模块。

mymodule = local_import(themodule)

这会在应用程序本地模块文件夹中导入名为 themodule 的模块,并使其在名称 mymodule 下可用。请注意,local_import 支持两个附加参数:reload 和 app。在开发过程中,模块代码经常会发生变化,因此请不要忘记使用参数 reload=True 告诉 web2py 在每次请求时重新加载模块,否则除非重新启动 web2py,否则您将看不到更改。

关于python - web2py 中使用 import 进行函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6557000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com