Permalink
Browse files

Show helpful error when user gives config name ending with '.py'

  • Loading branch information...
1 parent b0077db commit 977e2f7d58db5a79e673a28df8da115665825a43 @ask ask committed Jan 27, 2012
Showing with 23 additions and 2 deletions.
  1. +14 −1 celery/loaders/default.py
  2. +9 −1 celery/utils/__init__.py
View
@@ -16,12 +16,20 @@
from ..datastructures import AttributeDict
from ..exceptions import NotConfigured
-from ..utils import find_module
+from ..utils import find_module, NotAPackage
from .base import BaseLoader
DEFAULT_CONFIG_MODULE = "celeryconfig"
+CONFIG_INVALID_NAME = """
+Error: Module '%(module)s' doesn't exist, or it's not a valid \
+Python module name.
+"""
+
+CONFIG_WITH_SUFFIX = CONFIG_INVALID_NAME + """
+Did you mean '%(suggest)s'?
+"""
class Loader(BaseLoader):
"""The loader used by the default app."""
@@ -39,6 +47,11 @@ def read_configuration(self):
DEFAULT_CONFIG_MODULE)
try:
self.find_module(configname)
+ except NotAPackage:
+ if configname.endswith('.py'):
+ raise NotAPackage(CONFIG_WITH_SUFFIX % {
+ "module": configname, "suggest": configname[:-3]})
+ raise NotAPackage(CONFIG_INVALID_NAME % {"module": configname})
except ImportError:
warnings.warn(NotConfigured(
"No %r module found! Please make sure it exists and "
View
@@ -355,6 +355,10 @@ def cwd_in_path():
pass
+class NotAPackage(Exception):
+ pass
+
+
def find_module(module, path=None, imp=None):
"""Version of :func:`imp.find_module` supporting dots."""
if imp is None:
@@ -364,7 +368,11 @@ def find_module(module, path=None, imp=None):
last = None
parts = module.split(".")
for i, part in enumerate(parts[:-1]):
- path = imp(".".join(parts[:i + 1])).__path__
+ mpart = imp(".".join(parts[:i + 1]))
+ try:
+ path = mpart.__path__
+ except AttributeError:
+ raise NotAPackage(module)
last = _imp.find_module(parts[i + 1], path)
return last
return _imp.find_module(module)

0 comments on commit 977e2f7

Please sign in to comment.