16

当我./manage.py process_email在我的应用程序中执行时,我得到ImportError: No module named commands.process_email.

我的目录布局是:

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

process_email 命令的来源是:

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" 

和我得到的错误:

(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 

当我这样做时./manage.py,它确实显示process_email在“可用的子命令:”中。这告诉我,manage.py 看到了 process_email.py。init .py也是空的(我认为这无关紧要,仅供参考)。

4

1 回答 1

25

__init.py__命名正确吗?我不确定这是否是您的树中的错字,或者实际上是这样命名的。

Python将包含的目录__init__.py视为模块-但是,如果该文件不存在,它将不会处理该目录-因此它不会处理__init.py__或该“模块”。该文件为空白是完全可以的 - 但是,如果不是,您也可以从中导入(django 经常这样做)并且您也可以定义从模块中导出的内容。

于 2012-02-11T19:48:10.737 回答