[mod_python] Unable to import modules in subdirs

Graham Dumpleton grahamd at dscpl.com.au
Fri Oct 28 18:03:50 EDT 2005


On 29/10/2005, at 12:40 AM, Brandon N wrote:

> Are you forced, with mod_python, to write all package code within the 
> __init__.py?

As Jorey explained, that is a general requirement of Python. See:

   http://www.python.org/doc/essays/packages.html

> I ask because of what you've mentioned a few times now and what I keep 
> encountering.
>
>  I have Vampire up and running, though from a doc it specifies "...The 
> mechanism will also not work where a module is actually a package. Any 
> application specific modules should therefore not be structured as 
> packages..." which I imagine is a restriction coming from mod_python. 
> I have existing code that is already structured in various packages 
> and I'd like to keep it that way.

The bit of documentation you are referring to is the optionally enabled
feature of Vampire whereby use of the "import" statement will overlay
and make use of vampire.importModule() for a file based module within
the same directory of the document tree or selected other directories.

The vampire.importModule() mechanism only supports file based modules
and not full Python packages because it is not really possible to get
a depth checking module reloading mechanism to work with full Python
package based modules to work correctly.

This doesn't mean you cannot use full Python packages, you just can't
use them with vampire.importModule() and when you use the "import"
statement on modules local to the document tree with the overlay feature
enabled, any local Python package base packages will be ignored. If
however your Python package based module is kept in an area outside of
the document tree somewhere on the Python module search path, as perhaps
defined by the PythonPath directive, then "import" overlay will fall
back to using standard Python __import__ and find your package that way
and import it as Python would normally do.

In summary, put your Python package based module outside your document
tree and use PythonPath to specify where it is. Then use "import"
statement to import it.

>  I'm able to import Admin but Admin.RemoteAdmin fails (as before). The 
> only way I've found around this is via an import of RemoteAdmin within 
> Admin's __init__.py which isn't much of a fix.
>
>  With that klunky method I get the desired:
>  import Admin.RemoteAdmin as RA
>  A = RA.RemoteAdmin( )
>
>  Is there a more pythonic way to accomplish this from within 
> mod_python?
>
> On 10/27/05, Jorey Bump <list at joreybump.com > wrote:Brandon N wrote:
>>
>> > I meant seeing as others had pointed out the concern that one 
>> shouldn't
>> > put .py files under htdocs/ or similar directories for fear that 
>> someone
>> > might find access to one's source files, wholly intact.
>> >
>> > Though
>> >  > In order for
>> >  > Apache to make this determination, the .py files must be in the 
>> public
>> >  > directories that Apache is managing.
>> > made it clear for me.
>> >
>> > Is that at all a security issue. Or rather, is there a standard 
>> method
>> > of referencing code outside of the public directories?
>>
>> Yes. You import it, as you would with most Python applications. I use
>> Publisher, and typically have only a few lines of code in my published
>> modules, which act as interfaces to packages in my extended path:
>>
>> /var/www/vhosts/walamaloo/website/mp/six.py:
>>
>> """
>> Published module used as interface to rules package.
>> """
>> # all the code is in this package
>> # /var/www/vhosts/walamaloo/python/bruce/faculty/rules/__init__.py
>> import bruce.faculty.rules
>>
>> def index(req):
>>      """
>>      Default function for users.
>>      """
>>      return bruce.faculty.rules.four(req)
>>
>> def admin(req):
>>      """
>>      Requires authentication using proprietary mechanism.
>>      """
>>      return bruce.faculty.rules.onethreefiveseven(req)
>>
>>
>> This is just my preference. You can put all of your code in a 
>> published
>> module, if you want. But this approach allows me to hide sensitive
>> information outside of the DocumentRoot and encourages code reuse. I 
>> can
>> build apps relatively quickly because my extended path is filled with
>>  various utility modules.
>>
>> You might find some similarities to PHP's require() or include(), but
>> those are a bit simplistic compared to Python's import mechanism. On 
>> the
>> other hand, I have to restart apache often when developing new
>> applications, so PHP does have have one thing going for it (but only
>> one). :)
>>
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python




More information about the Mod_python mailing list