On Fri, Oct 30, 2009 at 03:55, mdipierro <[email protected]> wrote:
>
> Something always bothered be and resulted in lots of uglyness. I
> suspected there was a way to fix it but did not know. Now I found out.
>
> The problem:
> ==========
>
> when we do
>
> import a.b.c as d
>
> Python (and web2py) look in sys.path.
> If the module is in "web2py/applications/yourapp/modules" then
> "web2py/applications/yourapp/modules" should be added to sys.path.
> This would cause a major problem if there are many web2py apps that
> have a file a/b/c.py in modules. The import would find the first one,
> not necessarily the one in the current app.
> sys.path is not thread safe. There only a global sys.path not one per
> thread.
> So far the suggested solution was not do add the app modules path to
> sys.path and instead we used to do:
>
> exec('import applications.%s.modules.a.b.c as d' %
> request.application) # UGLY!
>
> This solves the conflict between app of modules but not conflicts with
> modules that are in sys.path.
> This limits which modules can go in the app modules/ folder because
> modules that user absolute imports cannot find their dependencies.
> This does not reload modules and one is forced to user conditional
> reloads when debugging modules.
>
> The solution
> =========
>
> I found and implemented a better way. With the code in trunk we can
> now do:
>
> d = local_import('a.b.c')
>
> - it is not based on exec
> - it searches in applicaitons/currentapp/modules/ before searching in
> sys.path so no conflicts ever
> - each modules/ folder acts like its own site-packages and you can put
> any third party module in there whether or not is uses relative
> imports
> - you can ask it force reloading modules at every request, great for
> debugging modules:
Hey, __import__ does this! What do we need a new function?
See:
In [1]: import matplotlib.pyplot as plt
In [2]: plt
Out[2]: <module 'matplotlib.pyplot' from
'/usr/lib/python2.5/site-packages/matplotlib/pyplot.pyc'>
In [3]: my_new_plt = __import__('matplotlib.pyplot', fromlist='pyplot')
In [4]: my_new_plt
Out[4]: <module 'matplotlib.pyplot' from
'/usr/lib/python2.5/site-packages/matplotlib/pyplot.pyc'>
> d = local_import('a.b.c', force=True)
>
> This opens the door to better plugins implemented (partially) as
> modules.
>
> Please check it out and report any success/failure. If ok it will be
> in 1.70.1
>
> Massimo
> >
>
--
Álvaro Justen
Diretor de Desenvolvimento
Peta5 - Nós fazemos TV digital
http://www.peta5.com.br/
21. 3021-6001 / 9898-0141
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---