Search code examples
pythondjangodjango-1.10

Import file from another app in Django 1.10


I'm trying to import a file from another app, and I get this error:

Unhandled exception in thread started by <function wrapper at 0x7fe7bc281d70>
Traceback (most recent call last):
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/apps/config.py", line 199, 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/carlos/www/lafam/apps/maquina/models.py", line 6, in <module>
    from apps.website.managers import GenericManager
ImportError: No module named website.managers

INSTALLED APPS in settings.py

SYSTEM_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

PROJECT_APPS = [
    'apps.website',
    'apps.maquina',
]

INSTALLED_APPS = SYSTEM_APPS + PROJECT_APPS

All my apps are in apps folder. I have an app called "maquina". And this is its modals.py

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from apps.website.managers import GenericManager
from .utils import path_and_rename


class Fabricante(models.Model):
    ...

I created a file called managers.py from another app called website

from django.db import models

class GenericManager(models.Manager):
    def get_or_none(self, *args, **kwargs):
        try:
            return self.get(*args, **kwargs)
        except self.model.DoesNotExist:
            return None
        except self.model.MultipleObjectsReturned:
            return None

I notice that the error is this line:

from apps.website.managers import GenericManager

This is my code structure:

enter image description here

How is the correct way to import a file from another app? Thanks!


Solution

  • in setting try to add this line after BASE_DIR sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) dont forget import os import sys