I totally agree with that. Though, I just read up, if pierreth is
trying to borrow code from app1/modules inside app2/controllers, then
injecting app1/modules into the python path should work. Of course,
this would just avoid having to put the module code inside app2/
modules (and would provide the ability inside the app2 controller to
use "import moudleA").

I do not rely on the technique (I use local_import), I just had an
idea with respect to this situation.

On Apr 6, 11:14 am, Jonathan Lundell <[email protected]> wrote:
> On Apr 6, 2011, at 8:55 AM, Mathew Grabau wrote:
>
>
>
> > It is not, strictly speaking, specific to web2py - it is specific to
> > any python script that uses the convention of calling into (executing
> > directory)/applications/app/modules/ for an import. It applies to
> > web2py, but web2py is breaking none of the rules there.
>
> > There are other ways to figure that out.
>
> > You can add to your python path (when executing under web2py): (web2py-
> > dir)/applications/myapp/modules.
>
> > With that in your python path (under web2py) it works.
>
> The reason this is problematical is that it exposes each modules directory to 
> the other web2py applications. For many (most?) cases that won't be a 
> problem, but if there are conflicting module names it won't work--that's why 
> local_import exists, yes?
>
>
>
>
>
>
>
>
>
> > On Apr 6, 10:43 am, pierreth <[email protected]> wrote:
> >> Yes, this is working. "applications" is in the path. But using
> >> "applications.yourappname.modules.modulename" is specific to web2py.
> >> Python developers are expecting to do "import modulename" otherwise,
> >> it will not work outside of web2py.
>
> >> On 6 avr, 10:55, [email protected] wrote:
>
> >>> Importing from modules works with absolute import , you must use
>
> >>>  import applications.yourappname.modules.modulename
>
> >>> The path has been set to the directory that web2py is running in - where 
> >>> the web2py.py file is.
> >>> Sent from my BlackBerry® smartphone on the MTS High Speed Mobility Network
>
> >>> -----Original Message-----
> >>> From: pierreth <[email protected]>
>
> >>> Sender: [email protected]
> >>> Date: Wed, 6 Apr 2011 07:39:47
> >>> To: web2py-users<[email protected]>
> >>> Reply-To: [email protected]
> >>> Subject: [web2py] Re: Modularity
>
> >>> Here are my conclusion on using the "modules" directory.
>
> >>> What is working:
>
> >>> - Using local_import avoid collisions of modules or packages that may
> >>> have the same names in other applications.
> >>> - Explicit and implicit relative imports are working well for modules
> >>> located in the "modules" directory.
>
> >>> What is not working:
>
> >>> - Absolute imports does not work because "modules" is not in the
> >>> Python path.
>
> >>> What this mean in practice:
>
> >>> - Modules located in "modules" can import each other if there are not
> >>> located in a package. This is implicit relative import.
> >>> - local_import is working well with both modules and packages.
> >>> - Modules located in "modules", regardless of if they are or not
> >>> located in a package, can only import other modules located both in a
> >>> package and in "modules" using explicit relative imports. Absolute
> >>> imports only work for modules and packages located in the Python path.
>
> >>> So if you drop both a module "a" depending on module "b" in "modules",
> >>> they will probably work very well. But if you drop both a package "a"
> >>> depending on package "b" (or a module "b"), it will probably not work.
> >>> Using relative imports in this case is not the norm. Inter packages
> >>> dependencies will not work in "modules". Is this a bug?
>
> >>> See:http://docs.python.org/tutorial/modules.html#packages
>
> >>> I suggest to add this message to the web2py book.

Reply via email to