Python – django manage.py how to put a project package on sys.path

django manage.py how to put a project package on sys.path… here is a solution to the problem.

django manage.py how to put a project package on sys.path

I read django doc and some SO posts to learn about manage.py and django-admin.py The difference between them.

They all say:

manage.py is automatically created in each Django project. manage.py
is a thin wrapper around django-admin.py that takes care of two things
for you before delegating to django-admin.py:

  1. It puts your project’s package on sys.path.
  2. It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to
    your project’s settings.py file.

So I checked the source code of both files (the latest version, so it’s documentation).

Then I’m confused. manage.py do the second thing: set the DJANGO_SETTINGS_MODULE environment variable. Other than that, I can’t really find any difference between these two scripts.

[django-admin.py]

#!/usr/bin/env python
from django.core import management

if __name__ == "__main__":
    management.execute_from_command_line()

[Manage .py].

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

Why? Are django documentation outdated? Or am I missing something here? Where is the code to put the project’s package on sys.path?

Solution

sys.path has been updated here Use a handle_default_ located at here options(options) statement. The execution path is as follows:

  • execute_from_command_line (argv) (your manage.py).
  • utility.execute() here
  • handle_default_options (Options). here

The Command class uses the same methods as the base class for administrative commands.

Related Problems and Solutions