Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24889 closed Bug (worksforme)

Multi dotted path ImportError

Reported by: Abdulaziz Alfoudari Owned by: nobody
Component: Core (Other) Version: 1.8
Severity: Normal Keywords: ImportError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a hierarchy of apps with different nesting levels:

├── apps
│   ├── accounts
│   ├── core
│   ├── modules
│   │   ├── companies
│   │   └── products

I'm encountering a weird behavior where importing multi dotted apps is raising an ImportError exception:

Traceback (most recent call last):
  File "./manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/aziz/projects/officeman/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/aziz/projects/officeman/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/home/aziz/projects/officeman/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/aziz/projects/officeman/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/aziz/projects/officeman/local/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/aziz/projects/officeman/django/apps/accounts/models.py", line 8, in <module>
    from apps.modules.companies.models import Company
ImportError: No module named modules.companies.models

Running python interpreter, I'm able to import all the different packages at all levels:

>>> from apps.modules import companies
>>> companies
<module 'apps.modules.companies' from 'apps/modules/companies/__init__.pyc'>
>>> 
>>> from apps import modules
>>> modules
<module 'apps.modules' from 'apps/modules/__init__.pyc'>
>>> 
>>> import apps
>>> apps
<module 'apps' from 'apps/__init__.pyc'>

The only reason I'm suspecting this to be a bug is the fact that python can import all the packages while Django can't. All apps imported fine in Django before I nested a group of them under modules.

Change History (3)

comment:1 by Tim Graham, 9 years ago

I suspect the problem is that your "apps" module is conflicting with "django.apps". Django is trying to import "from apps.modules.companies.models import Company" relative to "django.apps". Not sure if Django can do anything about this.

comment:2 by Abdulaziz Alfoudari, 9 years ago

Resolution: worksforme
Status: newclosed

It looks like it, although naturally django apps are imported with django.apps.app_name instead of a partial apps.app_name, so it would make more sense for import apps.app_name to look for the app in the project first and then revert back to third party packages. I'm not sure if this is within Django's control - namespace perhaps?

I'm closing the ticket and renaming the folder to something else other than apps.

comment:3 by Aymeric Augustin, 9 years ago

Perhaps from __future__ import absolute_import would help, but I'm not sure where to add it.

Note: See TracTickets for help on using tickets.
Back to Top