Set/change modules folder location for an app

62 views
Skip to first unread message

Alan Etkin

unread,
Dec 1, 2011, 8:30:22 AM12/1/11
to web2py-users
I would like to be able to change by a model/controller instruction to
change the path from where the modules are imported,

Suppose my web2py app is stored at ~/web2py/applications/myweb2pyapp

Instead making local_import("model") for a model stored at myweb2pyapp/
models/model.py, i need to make the same statement for a model stored
at ~/another/path/module.py

Is that possible? Note that i do not really know what local_import
does but i been told that it is required to update a modified module
without web server restart.

Thank you

Anthony

unread,
Dec 1, 2011, 8:47:42 AM12/1/11
to web...@googlegroups.com
First, you don't import models -- they are automatically executed by the framework. Instead, you import modules (which can be anywhere in the usual Python sys.path, or in your application's /modules folder).

Also, local_import has been deprecated. You can now import modules from your application's /modules folder by simply doing:

import mymodule  # assuming there is a mymodule.py in /applications/myapp/modules

To have the module reload automatically when there are changes, do:

from gluon.custom_import import track_changes
track_changes(True)

To import modules from another app:

from applications.otherapp.modules import mymodule


Anthony

Alan Etkin

unread,
Dec 1, 2011, 8:58:03 AM12/1/11
to web2py-users
>Instead making local_import("model") for a model stored at myweb2pyapp/
>models/model.py, i need to make the same statement for a model stored
>at ~/another/path/module.py

I meant import modules, sorry for the mistake.

So to import a module from another path i could do:

from gluon.custom_import import track_changes
track_changes(True)

sys.path.append("another/path")

Thank you Anthony

Anthony

unread,
Dec 1, 2011, 9:19:32 AM12/1/11
to web...@googlegroups.com
I don't think manipulating sys.path from your app code is recommended. Is the module in the modules folder of another app? If so, just do:

from applications.appname.modules import mymodule

Anthony 

Alan Etkin

unread,
Dec 1, 2011, 10:14:26 PM12/1/11
to web2py-users
The required modules are stored outside the web2py folder because they
are shared by a non web2py app. So i cannot import them with that
method. I know i could just copy them into the app's modules folder
and that would be it, But i do not want to duplicate files.

Massimo Di Pierro

unread,
Dec 2, 2011, 12:04:59 AM12/2/11
to web2py-users
you can create symbolic link to the web2py/site-packages folder or you
can add a sys.path.append to web2py.py

Alan Etkin

unread,
Dec 2, 2011, 6:30:51 AM12/2/11
to web2py-users
The symbolic link approach seems to me to be more interesting than
modifying web2py.py, but i do not see why the sys.path.append thing at
application code is not recomended. I'll have to search on that topic.

Thanks again for the support

On Dec 2, 2:04 am, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:

Alan Etkin

unread,
Dec 2, 2011, 6:41:14 AM12/2/11
to web2py-users
What do you know... i found this in Stackoverflow, wich covers my
question:

" ... In any multi-threaded Python program (and not only Python) you
should not use os.chdir and you should not change sys.path when you
have more than one thread running. It is not safe because it affects
other threads. Moreover you should not sys.path.append() in a loop
because it may explode.

All web frameworks are multi-threaded and requests are executed in a
loop. Some web frameworks do not allow you to install/un-install
applications without restarting the web server and therefore IF
os.chdir/sys.path.append are only executed at startup then there is no
problem.

In web2py we want to be able to install/uninstall applications without
restarting the web server. We want apps to be very dynamical (for
example define models based on information provided with the http
request). We want each app to have its own models folder and we want
complete separation between apps so that if two apps need to different
versions of the same module, they do not conflict with each other, so
we provide APIs to do so (request.folder, local_import).

You can still use the normal os.chdir and sys.path.append but you
should do it outside threads (and this is not a web2py specific
issue). You can use import anywhere you like as you would in any other
Python program.

I strongly suggest moving this discussion to the web2py mailing
list. ..."

Alan Etkin

unread,
Dec 2, 2011, 6:47:49 AM12/2/11
to web2py-users
Suppose now that i dont feel like changing the web2py.py script
because it is something like a framework's specific file. Then i think
it would be nice to be able to specify extra system routes to add to
the path on web service initialization, in an app specific way.

How about an extrapaths file stored somewhere inside the application
folder?

Massimo Di Pierro

unread,
Dec 2, 2011, 4:50:24 PM12/2/11
to web2py-users
You can do it in routes.py

import sys
sys.path.append(...)

that file is executed only once.

Alan Etkin

unread,
Dec 3, 2011, 9:34:45 AM12/3/11
to web2py-users
Perfect. I thought there was need for that extra file.

Thanks

On Dec 2, 6:50 pm, Massimo Di Pierro <massimo.dipie...@gmail.com>

Reply all
Reply to author
Forward
0 new messages