web2py 中使用 import 进行函数调用
我已将代码分成多个文件。我已将所有其他文件中的所有函数导入到 admin.py 中。假设我想调用一个函数 XYZ。如果我将函数路径指定为 admin/XYZ
,则会给出无效函数的错误,为此我必须将路径指定为 file_with_XYZ_function/XYZ
。
有没有一种方法可以克服这个问题并简单地从一个文件中调用所有导入的函数
I have split the code into multiple files. I have imported all the functions from all other files into admin.py. Lets say I want to call a function XYZ. If I give path to function as admin/XYZ
it gives me error as invalid function and for this I have to give the path as file_with_XYZ_function/XYZ
.
Is there a way to overcome this problem and simple call all the imported functions from one single file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:这可能无法回答您的问题,因为我不确定我是否理解您的问题...
但是如果您想将一些代码放入(共享)模块中并从多个控制器中包含它,我建议您有一个看看web2py书的第四章(核心)并搜索
本地导入
。模块的导入取决于模块以及 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,否则您将看不到更改。NOTE: This might not be answering your question as I'm not sure I understand your question...
But if you want to put some code into a (shared) module and include it from several of your controllers I suggest that you have a look at chapter four (The Core) of the web2py book and search for
local_import
.The import of modules depends on the modules and where web2py can find them. If it is a standard module which web2py can find in sys.path or in web2py/site-packages
import modulename
should work as expected.For modules local to your app web2py offers something else:
applications/appname/modules
Those modules can be imported using
local_import
.mymodule = local_import(themodule)
This imports the module with the name themodule in the apps local modules folder and makes it available under the name mymodule. Note that local_import supports two additional arguments: reload and app. During development module code often changes so don't forget to tell web2py to reload the module upon each request with the parameter
reload=True
, otherwise you won't see your changes unless you restart web2py.您可以在模块文件夹中创建 python 文件并导入它们,就像在控制器中导入 python 库一样。但是您必须提供这些文件的路径,就像
这是我的包装器的解决方案一样。现在您可以通过调用函数的名称来使用函数
you can create python files in modules folder and import them just like how you import python libraries in your controllers. But you have to give the path to those files like
this is my solution for my wrappers. now you can use your functions by calling their name