Opened 6 years ago

Closed 5 years ago

#13612 closed Bug (fixed)

manage.py imports settings.py even when --settings is used

Reported by: tonnzor Owned by: Graham King
Component: Core (Management commands) Version: master
Severity: Normal Keywords: manage.py django-admin.py settings
Cc: tonn81@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

manage.py file has an option to use settings file you need:

./manage.py runserver --settings=mycustomsettings

But it refuses running if settings file is not present:

Error: Can't find the file 'settings.py' in the directory containing './manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)

settings.py file should not be mandatory in that case!

I have a monkey-patch for manage.py file:

#!/usr/bin/env python

# HACK BEGIN
import sys
from optparse import OptionParser, make_option
settings_module = OptionParser(option_list=[make_option('--settings')]).parse_args(sys.argv[:])[0].settings
# HACK END

from django.core.management import execute_manager

if not settings_module:                     # HACK
    try:
        import settings # Assumed to be in the same directory.
    except ImportError:
        import sys
        sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
        sys.exit(1)
else:                                       # HACK
    settings = __import__(settings_module)  # HACK

if __name__ == "__main__":
    execute_manager(settings)

Attachments (1)

13612.diff (4.7 KB) - added by Graham King 5 years ago.
Try behaving like django-admin, only output error if that doesn't work. Also changed tests.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 6 years ago by Erik Romijn

Component: Uncategorizeddjango-admin.py
Needs documentation: unset
Needs tests: unset
Patch needs improvement: set
Version: 1.2SVN

comment:2 Changed 6 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

Agreed that this is a bit of a wart. It's also a case where we should be checking that the module exists, rather than importing and catching the import error.

comment:3 Changed 6 years ago by tonnzor

Cc: tonn81@… added
milestone: 1.3

comment:4 in reply to:  2 Changed 6 years ago by Ramiro Morales

Summary: Settings.py should not be mandatorymanage.py imports settings.py even when --settings is used

Replying to russellm:

... we should be checking that the module exists, rather than importing and catching the import error.

This behavior was changed in r15522. See also #10696.

comment:5 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

Changed 5 years ago by Graham King

Attachment: 13612.diff added

Try behaving like django-admin, only output error if that doesn't work. Also changed tests.

comment:6 Changed 5 years ago by Graham King

Easy pickings: unset
milestone: 1.31.4
Patch needs improvement: unset

If settings.py is not found, instead of issuing a warning message asking the user to run django-admin, couldn't we just do it for them? Only issue the error if that doesn't work.

The attached patch tries management.execute_from_command_line() before it issues the warning message.

That seems to make manage.py work for the three cases:

  • settings.py in current directory
  • settings on command line as --settings (and settings.py in current directory is optional)
  • settings in env variable DJANGO_SETTINGS_MODULE (and settings.py in current directory is optional)

A disadvantage is that if no settings.py is present in the current directory, and the settings file used throws an ImportError, the actual error is obscured by the warning message. However that message asks you to try django-admin.py, which does correctly display the error.

comment:7 in reply to:  6 Changed 5 years ago by Ramiro Morales

Patch needs improvement: set

Replying to graham_king:

A disadvantage is that if no settings.py is present in the current directory, and the settings file used throws an ImportError, the actual error is obscured by the warning message. However that message asks you to try django-admin.py, which does correctly display the error.

Another nit about the proposed patch is the one shown by the test in line 870 of the admin_script tests and the change you had to make to the expected manage.py output: If no settings.py file is present in the directory and the user doesn't specify one (either with the --setting option or by the DJANGO_SETTINGS_MODULE envvar) then the error shown is (e.g. in the test case) Unknown command: 'noargs_command'. No hint is given to the user about the fact that she is trying to run manage.py without a settings file at all it the reason of the failure.

comment:8 Changed 5 years ago by Graham King

Keywords: manage.py django-admin.py settings added
Owner: changed from nobody to Graham King
Status: newassigned

comment:9 Changed 5 years ago by Jacob

milestone: 1.4

Milestone 1.4 deleted

comment:10 Changed 5 years ago by Claude Paroz

Resolution: fixed
Status: assignedclosed
UI/UX: unset

This is now solved with the new manage.py version introduced in r16964.

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