Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

function - How can I use modules in web2py?

I have some functions in func.py that I would like to access from my web2py controller called default.py. I tried to import and use func.py in default.py with "import func" and "calculatesomething = func.calculatesomething", but it doesn't work. The file func.py is also added in the Modules section of my web2py program.

Why it is not working? How can I make it to work? Should the module be in func.py or default/func.py or something else?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just to add a bit to user570039's answer, local_import is documented here. It includes a reload parameter. According to the documentation:

When you specifyreload=True, it will re-import the module upon each request; otherwise your python process will only import the module once. The default isreload=False.

In development, setting reload=True can be convenient because changes to your module will work immediately without requiring a restart. However, re-importing upon each request will slow down performance, so reload=False is recommended in production.


UPDATE: Things have changed. local_import has been deprecated. If you have /applications/myapp/modules/mymodule.py, you can import it within myapp by doing:

import mymodule

web2py will check the application's "modules" folder before checking the rest of sys.path.

For more details, see here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...