16

When I do ./manage.py process_email in my app, I get ImportError: No module named commands.process_email.

My directory layout is:

./
├── __init__.py
├── admin.py
├── forms.py
├── management
│   ├── __init__.py
│   └── commands
│       ├── __init.py__
│       └── process_email.py
├── models.py
├── views.py

The source of the process_email command is:

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from website.event.models import Event

class Command(BaseCommand):

    def handle(self, *args, **options):
        process_email()

def process_email():
    print "processing email" 

and the error I'm getting:

(website.com)kings@bob-kings-MacBook ~/code/website.com/website $  > ./manage.py  process_email
Traceback (most recent call last):
  File "./manage.py", line 14, in <module>
    execute_manager(settings)
  File "/Users/kings/code/website.com/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/Users/kings/code/website.com/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/kings/code/website.com/lib/python2.6/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/kings/code/website.com/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Users/kings/code/website.com/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named commands.process_email 

when I do ./manage.py, it does show process_email in the "Available subcommands:". This tells me that process_email.py is seen by manage.py. Also init.py is empty (I do not think it matters but just FYI).

Trewq
  • 3,187
  • 6
  • 32
  • 50
  • 1
    hello, welcome to SO. Just a pointer - don't worry about using pastebin, you're welcome to put your code / directory layout in here (in fact it helps us if you do). –  Feb 11 '12 at 19:45
  • As a warning, Django is using imp.find_module which does not respect zipped eggs: http://stackoverflow.com/questions/28962344/imp-find-module-which-supports-zipped-eggs – benjaoming Apr 06 '15 at 14:12

1 Answers1

25

Is __init.py__ named correctly? I am not sure if that's a typo in your tree, or actually named like that.

Python treats directories containing __init__.py as modules - however, if that file does not exist it will not process that directory - so it won't process __init.py__ or that "module". It is perfectly ok for that file to be blank - however, if it isn't you can import from it too (django does this a lot) and you can define what gets exported from the module too.

  • Thank you. Yes, I had misnamed the file. I appreciate your pointers with SO, and will put the code in place in the future.. I personally find it annoying when there is a lot code in the question, and the essence of the question is lost :) – Trewq Feb 11 '12 at 22:09
  • 1
    @Trewq no problem - I try to keep the code down to where I think the error is - I then restate the problem at the bottom of the question for clarity so that it isn't lost after all that code! –  Feb 11 '12 at 22:20