Home > Net >  Yet another question why django can't see the app
Yet another question why django can't see the app

Time:08-09

I want all the apps to be in the same shared apps folder. But django settings doesn't want to see the created app.

how I created the app

  1. Created an empty apps folder in the project root directory.
  2. Created an empty __init__.py file in it
  3. Created an empty folder inside apps folder - creative_performer
  4. Run the command python manage.py startapp creative_performer apps/creative_performer

Got the following structure

/gpanel
    /projects
        urls.py
        settings.py
        ....
     /apps
        __init__.py
            /creative_performer
                /migrations
                __init__.py
                apps.py
                models.py
                ....

manage.py

settings.py

PROJECT_APPS = ['creative_performer']

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]   PROJECT_APPS

But after server run got

  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'creative_performer'

I have also tried creating an app with the command

django-admin startapp creative_performer

Same result

If create an app in the root of the project, without using the apps shared folder, everything starts working

Else tried change PROJECT_APPS to PROJECT_APPS = ['apps.creative_performer']

But got

django.core.exceptions.ImproperlyConfigured: Cannot import 'creative_performer'. Check that 'apps.creative_performer.apps.CreativePerformerConfig.name' is correct.

But CreativePerformerConfig.name == 'creative_performer' , it's not clear why the error occurs

P.S.

Resolved by change apps.py changing name = 'apps.creative_performer'

and settings.py

PROJECT_APPS = ['apps.creative_performer']

But, is this normal django behaviour ? Is it really impossible to create apps inside a shared folder without changing the default settings?

CodePudding user response:

Django's installed_apps uses Python's normal import system to locate the installed apps. At the end of the day, this is what matters: that your apps can be located and imported, which follows the python packaging and import semantics. So, for most third-party apps you install via pip are already available on the PYTHONPATH to be imported as a top-level name.

Because your app creative_performer is actually a subpackage under apps, you need to use 'apps.creative_performer' in your installed_apps and set the app name to the same to use it in this case.

Another option would be to remove the apps/__init__.py file and add the apps directory to your PYTHONPATH environment variable (or modify sys.path) to deal with this. That way, your apps are just standalone packages. But the correct thing to do would probably be to leave it as you have it with 'apps.creative_performer' -- which is similar to what django does in its contrib subpackages, for example. Generally, modifying your pythonpath is discouraged.

Yet another option would be to distribute your apps as reusable apps and install them separately altogether.

  • Related