11

I've written a simple custom command, hello.py:

from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    help = "prints hello world"

    def handle_noargs(self, **options):
        print "Hello, World!"

When I run python manage.py hello it returns

Unknown command: 'hello'

  • I've put it in the management/commands directory beneath my app.
  • I've added __init__.py files to the management and commands directory.
  • I've checked my app is in INSTALLED_APPS in settings.py
  • I've tried installing it in different apps and from the project root too

Running python manage.py syncdb etc is fine. And if I type python at the command line I can import django.core.management ok.

I know I'm missing something obvious, but can't figure out what.

How can I debug this to work out why my custom command won't run?

5
  • 2
    By 'checked my app is in settings.py', do you mean it is in INSTALLED_APPS? Commented Nov 28, 2010 at 10:37
  • Do you have multiple Python versions/installations on your system? 'which python' will tell you what python version you are using when running 'python manage.py', running 'env python' will start the python that './manage.py' would start. Is it the same version? Commented Nov 28, 2010 at 10:39
  • @knutin. Yes it's in INSTALLED_APPS. Yes multiple versions and which python returns alias python='python2.6'/usr/local/bin/python2.6. However, env python seems to start Python 2.4.3 and import django soesn't work when I run env python. However, when I run python manage.py I assume I am running the correct python and it still doesn't work. Commented Nov 28, 2010 at 10:44
  • Ok, so you have installed Django in your 2.6 site-packages, but not in 2.4.3. When running your own python, always make sure you are really using the right one, especially when deploying your site. Commented Nov 28, 2010 at 10:47
  • Yes when I run python it does run 2.6. Still not working though. Commented Nov 28, 2010 at 10:54

3 Answers 3

8

The problem was that I had another project on my PYTHONPATH. D'oh! I think it was picking up the settings.py from there first so didn't see my app. What pointed me in this direction was I tried running python manage.py create_jobs myapp (from django command extensions) and it returned an error indicating the app couldn't be found. Also @knutin mentioned INSTALLED_APPS.

Sign up to request clarification or add additional context in comments.

1 Comment

My custom command mysteriously began to work after doing pip install command_extensions in my virtualenv. I was trying to retrace your debugging steps for my "Unknown command" problem. Somehow the pip installer or the command extensions module itself must have cleaned up my PYTHONPATH or DJANGO_SETTINGS_MODULE problem.
0

To solve the issue: I move the management folder one level up in my folder tree. In other words, in the same folder as settings.py

Comments

-2

It is because the __init__.pyc does not get created automatically within "management" and "commands" folder. Copy your_app/__init__.py and your_app/__init__.pyc and paste it within the management/ and commands/ folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.