Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14130 -- Made manage.py error reporting more useful when the s…

…ettings.py file triggers import errors (in new projects). Thanks Setok for the report, mk and steph for their work.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15522 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a797b7dba0644558ca7bf825b22268f2b4d9d3b5 1 parent c7618d5
Ramiro Morales authored February 13, 2011
7  django/conf/project_template/manage.py
... ...
@@ -1,11 +1,14 @@
1 1
 #!/usr/bin/env python
2 2
 from django.core.management import execute_manager
  3
+import imp
3 4
 try:
4  
-    import settings # Assumed to be in the same directory.
  5
+    imp.find_module('settings') # Assumed to be in the same directory.
5 6
 except ImportError:
6 7
     import sys
7  
-    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__)
  8
+    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" % __file__)
8 9
     sys.exit(1)
9 10
 
  11
+import settings
  12
+
10 13
 if __name__ == "__main__":
11 14
     execute_manager(settings)
29  tests/regressiontests/admin_scripts/tests.py
@@ -958,6 +958,35 @@ def test_custom_command_with_environment(self):
958 958
         self.assertNoOutput(out)
959 959
         self.assertOutput(err, "Unknown command: 'noargs_command'")
960 960
 
  961
+class ManageSettingsWithImportError(AdminScriptTestCase):
  962
+    """Tests for manage.py when using the default settings.py file
  963
+    with an import error. Ticket #14130.
  964
+    """
  965
+    def setUp(self):
  966
+        self.write_settings_with_import_error('settings.py')
  967
+
  968
+    def tearDown(self):
  969
+        self.remove_settings('settings.py')
  970
+
  971
+    def write_settings_with_import_error(self, filename, apps=None, is_dir=False, sdict=None):
  972
+        test_dir = os.path.dirname(os.path.dirname(__file__))
  973
+        if is_dir:
  974
+            settings_dir = os.path.join(test_dir,filename)
  975
+            os.mkdir(settings_dir)
  976
+            settings_file = open(os.path.join(settings_dir,'__init__.py'), 'w')
  977
+        else:
  978
+            settings_file = open(os.path.join(test_dir, filename), 'w')
  979
+        settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n')
  980
+        settings_file.write('# The next line will cause an import error:\nimport foo42bar\n')
  981
+
  982
+        settings_file.close()
  983
+
  984
+    def test_builtin_command(self):
  985
+        "import error: manage.py builtin commands shows useful diagnostic info when settings with import errors is provided"
  986
+        args = ['sqlall','admin_scripts']
  987
+        out, err = self.run_manage(args)
  988
+        self.assertNoOutput(out)
  989
+        self.assertOutput(err, "ImportError: No module named foo42bar")
961 990
 
962 991
 class ManageValidate(AdminScriptTestCase):
963 992
     def tearDown(self):

0 notes on commit a797b7d

Please sign in to comment.
Something went wrong with that request. Please try again.