Well, like almost everything in Python, the import system is hack-able. You just need to create a custom loader and register it at sys.meta_path
(for details see PEP 302).
Lets say you want to hack the import system in order to load "foo.bar" if you import "foo_dot_bar":
# search folder "foo.bar" and load it as a package
from foo_dot_bar import *
Be warned: this is just a starting point for you, it is not a fully tested solution; in fact it is way beyond my wizardry level!
# stupid_dot_importer.py
import os
import imp
import sys
class StupidDotPackageLoader(object):
@staticmethod
def _get_real_name(name):
return ".".join(name.split('_dot_'))
def find_module(self, name, path=None):
try:
imp.find_module(self._get_real_name(name))
except ImportError:
return None
return self
def load_module(self, name):
_, pathname, description = imp.find_module(self._get_real_name(name))
return imp.load_module(self._get_real_name(name), None, pathname, description)
Suppose you have the following structure:
foo.bar
|
+--- __init__.py
|
+--- module1.py
|
+--- module2.py
And:
$ cat foo.bar/__init__.py
from module1 import *
from module2 import *
$ cat foo.bar/module1.py
foo = 'bar'
$ cat foo.bar/module2.py
spam = 'eggs'
Then magic:
>>> from stupid_dot_importer import *
>>> sys.meta_path = [StupidDotPackageLoader()]
>>> from foo_dot_bar import *
>>> foo
'bar'
>>> spam
'eggs'
>>>