List apps of a django project
Asked Answered
E

3

6

I'm completely new for django and I'd want to list the apps of a django project, for example:

FeinCMS

I know that startapp creates the directory structure for an app. I wonder if there is either a function or a file to get the app list.

For example taking FeinCMS as an example, the repo contains:

feincms
├── AUTHORS
├── CHANGELOG.rst
├── CONTRIBUTING.rst
├── docs
│   ├── admin.rst
│   ├── advanced
│   ├── conf.py
│   ├── contenttypes.rst
│   ├── contributing.rst
│   ├── deprecation.rst
│   ├── extensions.rst
│   ├── faq.rst
│   ├── images
│   ├── index.rst
│   ├── installation.rst
│   ├── integration.rst
│   ├── Makefile
│   ├── medialibrary.rst
│   ├── migrations.rst
│   ├── page.rst
│   ├── releases
│   ├── settings.rst
│   ├── templatetags.rst
│   └── versioning.rst
├── feincms
│   ├── admin
│   ├── apps.py
│   ├── content
│   ├── contents.py
│   ├── context_processors.py
│   ├── contrib
│   ├── default_settings.py
│   ├── extensions
│   ├── __init__.py
│   ├── _internal.py
│   ├── locale
│   ├── management
│   ├── models.py
│   ├── module
│   ├── shortcuts.py
│   ├── signals.py
│   ├── static
│   ├── templates
│   ├── templatetags
│   ├── translations.py
│   ├── urls.py
│   ├── utils
│   └── views
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.cfg
├── setup.py
└── tests
    ├── cov.sh
    ├── manage.py
    ├── requirements.txt
    ├── testapp
    └── tox.ini

How can I use django to list the apps in the repo?

Empiricism answered 21/6, 2018 at 13:59 Comment(1)
Check this as put forward by @nttaylor in comments section of the accepted solution.Gausman
P
3

settings.INSTALLED_APPS is the list of apps. You can start a django shell (manage.py shell) to query the settings:

from django.conf import settings
print(settings.INSTALLED_APPS)

>>> ['user', 'django.contrib.auth', 'django.contrib.sites', ...]
Pump answered 21/6, 2018 at 14:6 Comment(2)
It's a nice answer, however, this the case when you are developing a project. But how can I load an existing project in django and list its apps? I will clarify my question.Empiricism
You can't. You need to create an app that uses the FeinCMS and then run it (in the shell). The testapp in the tests directory will allow you to do that without having to create an app yourself.Pump
R
3

Query django.apps ...

from django.apps import apps
for app in apps.get_app_configs():
       print(app, app.name, app.label)

Results in ...

<ContentTypesConfig: contenttypes> django.contrib.contenttypes contenttypes
<AdminConfig: admin> django.contrib.admin admin
Rhys answered 21/6, 2018 at 14:16 Comment(1)
this is a much better approach because often you care about the app label (such as running migrate or makemigrations)Polygynist
M
0

A larger project can contain multiple Django apps which can reside deep inside it's directory structure. Django provides the apps registry, which can return the list of all apps known to the current Django site. However, the apps registry can only return the list of installed apps (e.g. defined in INSTALLED_APPS in your settings). Apps that are not installed can not be discovered in that way.

But there is another way, for discovering all Django apps in a directory tree, both installed and uninstalled. A Django app usually has an AppConfig definition in a apps.py file. To find all apps you can look for apps.py files and extract the information like name and label from the AppConfig definition inside it.

I had the same problem and made a command line tool that does exactly that.

Please note that this method does not work in all cases. While uncommon, it is technically possible to define Django apps without an apps.py file. The script does not cover that case.

Meddle answered 4/10, 2024 at 13:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.