[mod_python] mod_python imports foo/__init__.py, not foo/bar.py

Graham Dumpleton grahamd at dscpl.com.au
Sat Oct 15 21:06:15 EDT 2005


On 16/10/2005, at 10:24 AM, Fabiano Sidler wrote:

> Wow, what a fast answer!
>
> Graham Dumpleton wrote:
>>    What If I Have a Module and a Package With The Same Name?
>
> No, I have:
>  foobar.py
>  dir/foobar.py
> which is surely inapt, but these two modules aren't imported to the 
> same
> script at the same time. And right now I found out that top-level 
> modules
> can be imported without problems (i.e. foobar).

Sorry, perhaps misread your question, the way you expressed the problem
was a bit confusing, so thought you were saying it was importing
foo/bar/__init__.py instead of foo/bar.py, which would be the package
import precedence rule coming into play.

Your original question is as below, perhaps you can clarify further what
the actual problem is, describing the context you are seeing it in. Have
mentioned a few points though.

> When I try importing a module 'foo.bar', mod_python imports 
> foo.__init__.py
> instead of foo.bar! Why so?

You say importing "foo.bar". Was this as:

   import foo.bar

If it is and "foo" is a package, Python will first import the file:

   foo/__init__.py

Having done that it will then import the file:

   foo/bar.py

It has to download packages starting at the root like this even though
you are after a subcomponent only. After the import, you could access
both the "foo" and "foo.bar" modules.

   foo.__dict__
   foor.bar.__dict__

Note that this presumes "bar" as a subcomponent of "foo" is in turn a
module. If it wasn't and "foo.bar" is something else, there should have
been an import error.

The linked document talks about how packages are loaded in this way.

Now in your followup you said:

> No, I have:
>  foobar.py
>  dir/foobar.py

This has just got me more confused about what the problem is as you are
mentioning files which would not be associated with "foo.bar". If I am
to make assumptions and assume that you are using mod_python.publisher
I might guess that you are running up against bug:

   http://issues.apache.org/jira/browse/MODPYTHON-9

This is where once you have access "foobar" in the subdirectory, it does
not reload "foobar" from the parent properly. This is fixed in 
mod_python
version 3.2.

Anyway, sorry again for confusion, was trying to be helpful and provide
some help on what was an unclear problem description. You might want to
try and be clearer about the problem then I might be able to give a
sensible answer. ;-)

Graham



More information about the Mod_python mailing list