I won't get into the polemics on renaming and instead focus on showing you how to do what you want (whether it's "good for you" or not;-). The solution is not difficult...
Just set __path__
! A little demonstration:
$ mkdir /tmp/modules /tmp/packages
$ mkdir /tmp/packages/openid
$ echo 'print "Package!"' > /tmp/packages/openid/__init__.py
$ gvim /tmp/modules/openid.py
$ PYTHONPATH='/tmp/modules:/tmp/packages' python -c'import openid'
Module!
Package!
this shows a module openid managing to import a homonymous package even though the module's path comes earlier in sys.path, and sys.modules['openid']
is clearly already set at that time. And all the "secret" is in openid.py's simple code...:
print "Module!"
__path__ = ['/tmp/packages']
import openid
without the __path__
assignment, of course, it would only emit Module!
.
Also works for importing submodules within the package, of course. Do:
$ echo 'print "Submod!"' > /tmp/packages/openid/submod.py
and change openid.py's last line to
from openid import submod
and you'll see:
$ PYTHONPATH='/tmp/modules:/tmp/packages' python -c'import openid'
Module!
Package!
Submod!
$