Opened 15 years ago

Closed 13 years ago

#13612 closed Bug (fixed)

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

Reported by: Artem Skoretskiy Owned by: Graham King
Component: Core (Management commands) Version: dev
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 14 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 by Sasha Romijn, 15 years ago

Component: Uncategorizeddjango-admin.py
Patch needs improvement: set
Version: 1.2SVN

comment:2 by Russell Keith-Magee, 15 years ago

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 by Artem Skoretskiy, 14 years ago

Cc: tonn81@… added
milestone: 1.3

in reply to:  2 comment:4 by Ramiro Morales, 14 years ago

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 by Julien Phalip, 14 years ago

Severity: Normal
Type: Bug

by Graham King, 14 years ago

Attachment: 13612.diff added

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

comment:6 by Graham King, 14 years ago

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.

in reply to:  6 comment:7 by Ramiro Morales, 14 years ago

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 by Graham King, 14 years ago

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

comment:9 by Jacob, 13 years ago

milestone: 1.4

Milestone 1.4 deleted

comment:10 by Claude Paroz, 13 years ago

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