I have modules in the project with the same names, all placed in different applications.
Particularly there are two apps that conflict and result an ImportError. The project structure approximately is as follows.
project
|_ project
|_ settings.py
|_ ...
|_ apps
|_ A
|_ handlers
|_ B.py
|_ C.py
|_ B
|_ models.py
The settings file adds apps
directory to the system path.
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(BASE_DIR, "apps"))
This configuration assumes that when I import the B.models
it will use apps/B/models.py
. Although the following line in the C.py
file raise an import error as it imports the A/handlers/B.py
which does not have models
module.
A/handlers/C.py:
from B.models import BModel
The sys.path
variable has a correct items, i.e. the first one in the list is /path/to/project/apps
and sys.modules['B.models']
is referenced to the correct file.
BTW, when I use Django's import_by_path
function, everything works fine.
Any ideas how I can solve the problem without renaming the modules?
Posted sample sources on GitHub.
Thanks in advance!