Problematic behavior of the import statement when several modulefiles have the same name.

Robert Brewer fumanchu at
Wed Mar 9 19:28:07 CET 2005

Xif wrote:
> Here's a problem with relative imports:
> Suppose I have a package called some_package (in a separate directory
> included in the PYTHONPATH, with an file etc.)
> This package has a module inside the directory, called "database", and
> therefore  residing in the file some_package/
> Now, what if there's another module, for example inside the
> site-packages directory, with the same file name (i.e.
> We have a problem. Since although these modules have 
> different absolute
> names ("some_package.database" for the first module, and just
> "database" for the second), when I try to do
> import database
> from inside some_package, it first of all tries to find the matching
> file in the some_package directory (i.e. do a relative import). Since
> it first checks the some_package directory, and finds
> there,
> import database
> in fact imports the module with the absolute name
> some_package.database.
> This is problemat on two levels:
> 1) It is surprising and undesirable that "import database" in effect
> may do two completely different things ("import some_package.database"
> or "import database") depending on an external, unpredictable factor:
> the existence of a file inside the some_package directory.
> 2) It effectively prevents you from naming a module inside a package
> with the same name of any module in the "root" PYTHONPATH directories.
> In my example, there's no sane way I can see of having
> some_package.database if there's already a database module 
> (
> file) in any PYTHONPATH directory.
> Are my observations correct?  Is there something I ignored?  Should
> this be posted somewhere else?

Short answer: don't use relative imports:

from some_package import database

Robert Brewer
Amor Ministries
fumanchu at

More information about the Python-list mailing list