Opened 15 years ago
Closed 14 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)
Change History (11)
comment:1 by , 15 years ago
| Component: | Uncategorized → django-admin.py |
|---|---|
| Patch needs improvement: | set |
| Version: | 1.2 → SVN |
follow-up: 4 comment:2 by , 15 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 15 years ago
| Cc: | added |
|---|---|
| milestone: | → 1.3 |
comment:4 by , 15 years ago
| Summary: | Settings.py should not be mandatory → manage.py imports settings.py even when --settings is used |
|---|
comment:5 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Bug |
by , 14 years ago
| Attachment: | 13612.diff added |
|---|
Try behaving like django-admin, only output error if that doesn't work. Also changed tests.
follow-up: 7 comment:6 by , 14 years ago
| Easy pickings: | unset |
|---|---|
| milestone: | 1.3 → 1.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 by , 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 , 14 years ago
| Keywords: | manage.py django-admin.py settings added |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
Asking about which way to proceed here: http://groups.google.com/group/django-developers/browse_thread/thread/7a56b452ec671943
comment:10 by , 14 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
| UI/UX: | unset |
This is now solved with the new manage.py version introduced in r16964.
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.