在web2py中使用import调用函数 [英] Function call using import in web2py
问题描述
我已将代码分成多个文件。我已将所有其他文件的所有功能导入到admin.py中。可以说我想调用一个函数XYZ。如果我为> admin / XYZ
给出路径,它会给我提供错误,因为它是无效函数,为此我必须将路径设为 file_with_XYZ_function / XYZ
。
有没有办法解决这个问题,并简单地从一个文件中调用所有导入的函数
<注意:这可能不会回答你的问题,因为我不知道我理解你的问题......
但是,如果你想把一些代码放到一个(共享)模块中,并将它包含在你的几个控制器中,我建议你看看的第四章( )。 href =http://web2py.com/book/default/chapter/04 =nofollow> web2py book 并搜索
local_import
。
对于这种情况,web2py
提供了另一种导入模块
的方式,即全局sys .path
没有改变:将它们放在
modules文件夹中o f应用程序。
一个好处是模块
将被自动复制,而
将随应用程序一起分发;
但是,有一些
限制适用。 web2py
提供了一个local_import函数,必须使用
从
中导入模块到modules文件夹中。
模块的导入取决于模块以及web2py可以找到它们的位置。如果它是web2py在sys.path或web2py / site-packages中找到的标准模块 import modulename
应该按预期工作。
对于您的应用程序本地的模块,web2py提供了其他功能: applications / appname / modules
这些模块可以使用 local_import
。
$ b
mymodule = local_import(themodule) code>
这将在名为mymodule的应用程序本地模块文件夹中导入名为themodule的模块并使其可用。请注意,local_import支持另外两个参数:reload和app。在开发过程中,模块代码经常发生变化,所以不要忘记告诉web2py在每次请求时使用参数 reload = True
重新加载模块,否则除非另有说明,否则不会看到更改你重新启动web2py。
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
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
.
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.
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.
这篇关于在web2py中使用import调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!