I am trying to make a custom management command as show in the docs here: https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
我试图在这里的文档中显示自定义管理命令:https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
When I try to run the command from my project directory I am experiencing the following error:
当我尝试从项目目录运行命令时,遇到以下错误:
AttributeError: 'module' object has no attribute 'Command'
Here is the file:
这是文件:
#event_expiration.py
from django.core.management.base import BaseCommand, CommandError
from app.models import Event
import datetime
class Command(BaseCommand):
help = 'deletes expired events'
def handle(self, *args, **options):
today = datetime.datetime.now()
events = Event.objects.filter(date=datetime.date(2011,11,11))
for e in events:
e.delete()
self.stdout.write('Expired events successfully deleted.')
The command I am running is :
我正在运行的命令是:
$ python manage.py event_expiration
I've made sure I am adding the event_expiration.py file within management and commands folders and that those folders have init files. those are in one of my app folders.
我确保在管理和命令文件夹中添加event_expiration.py文件,并确保这些文件夹具有init文件。那些在我的app文件夹中。
Am I overlooking something here? Any help is appreciated, thanks!
我在这里俯瞰什么吗?任何帮助表示赞赏,谢谢!
EDIT:
编辑:
Fellow SO user Yuji helped me attempt to debug this a bit but we are still stumped. heres what we did:
SO用户Yuji帮助我尝试调试这一点,但我们仍然难过。继承人我们做了什么:
First, the full traceback and command:
首先,完整的回溯和命令:
(venv)matt@inspirion14z:~/Dropbox/PROD/ersvp.it$ python manage.py event_expiration
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 70, in load_command_class
return module.Command()
AttributeError: 'module' object has no attribute 'Command'
To see what was going on at django/core/management/init.py", line 70 I placed import pdb; pdb.set_trace() within the file.
要查看django / core / management / init.py上发生了什么,第70行我在文件中放置了import pdb; pdb.set_trace()。
While in debug mode we tried:
在调试模式下,我们尝试:
module.__file__
to check if the module was where expected, and it indeed was, with an output of:
检查模块是否在预期的位置,确实是,输出为:
'/home/matt/Dropbox/PROD/ersvp.it/app/management/commands/event_expiration.pyc'
Next, we tried manually importing Command in the shell:
接下来,我们尝试在shell中手动导入Command:
>>> from app.management.commands.event_expiration import Command
Traceback (most recent call last): File "<console>", line 1, in <module> ImportError: cannot import name Command
Still scratching my head!
还在挠我的头!
2 个解决方案
#1
3
What is your file structure like? It should be like so:
你的文件结构是什么样的?它应该是这样的:
app/
__init__.py
management/
__init__.py
commands/
__init__.py
event_expiration.py
If the structure is as above, try the following:
如果结构如上,请尝试以下操作:
python manage.py shell
>>> from app.management.commands import event_expiration
>>> dir(event_expiration)
['Account', 'BaseCommand', 'Callback', 'Command', 'CommandError', 'Comment', 'Status', 'User', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'clean_phone_number', 'csv', 'models', 'os', 're']
I've listed the pure output of running dir
on a management command of my own. Give that a try, and report back what is available to the module. You might find yourself getting an error at this point, which may help diagnose. I'm suspecting a problem with importing django itself. I'm guessing the python manage.py shell
will fail, which will mean it's not a problem with your command, but a problem with the project.
我已经在我自己的管理命令中列出了运行目录的纯输出。尝试一下,并报告模块可用的内容。此时您可能会发现自己收到错误,这可能有助于诊断。我怀疑导入django本身有问题。我猜python manage.py shell会失败,这意味着它不是你的命令的问题,而是项目的问题。
Edit 2:
编辑2:
The fact that check_expiration
was visible in your dir
output supports my theory that the folder structure is amiss in someway. Unless there's specifically a function named that within your module.
check_expiration在你的dir输出中可见这一事实支持了我的理论,即文件夹结构在某种程度上是不对的。除非在您的模块中特别指定了一个函数。
Please do the following and show the output:
请执行以下操作并显示输出:
cd /path/to/app/
find .
Also, show the entire contents of your event_expiration.py
file, and the contents of your management/commands/__init__.py
file. Be wary of spaces mixed with tabs as whitespace also.
另外,显示event_expiration.py文件的全部内容以及management / commands / __ init__.py文件的内容。警惕与标签混合的空格也是空白。
#2
21
I ran into the same issue and the problem was that my command class wasn't called exactly Command
, as the docs says. Example:
我遇到了同样的问题,问题是我的命令类没有像文件那样被称为Command。例:
class Command(NoArgsCommand):
# Do something here
#1
3
What is your file structure like? It should be like so:
你的文件结构是什么样的?它应该是这样的:
app/
__init__.py
management/
__init__.py
commands/
__init__.py
event_expiration.py
If the structure is as above, try the following:
如果结构如上,请尝试以下操作:
python manage.py shell
>>> from app.management.commands import event_expiration
>>> dir(event_expiration)
['Account', 'BaseCommand', 'Callback', 'Command', 'CommandError', 'Comment', 'Status', 'User', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'clean_phone_number', 'csv', 'models', 'os', 're']
I've listed the pure output of running dir
on a management command of my own. Give that a try, and report back what is available to the module. You might find yourself getting an error at this point, which may help diagnose. I'm suspecting a problem with importing django itself. I'm guessing the python manage.py shell
will fail, which will mean it's not a problem with your command, but a problem with the project.
我已经在我自己的管理命令中列出了运行目录的纯输出。尝试一下,并报告模块可用的内容。此时您可能会发现自己收到错误,这可能有助于诊断。我怀疑导入django本身有问题。我猜python manage.py shell会失败,这意味着它不是你的命令的问题,而是项目的问题。
Edit 2:
编辑2:
The fact that check_expiration
was visible in your dir
output supports my theory that the folder structure is amiss in someway. Unless there's specifically a function named that within your module.
check_expiration在你的dir输出中可见这一事实支持了我的理论,即文件夹结构在某种程度上是不对的。除非在您的模块中特别指定了一个函数。
Please do the following and show the output:
请执行以下操作并显示输出:
cd /path/to/app/
find .
Also, show the entire contents of your event_expiration.py
file, and the contents of your management/commands/__init__.py
file. Be wary of spaces mixed with tabs as whitespace also.
另外,显示event_expiration.py文件的全部内容以及management / commands / __ init__.py文件的内容。警惕与标签混合的空格也是空白。
#2
21
I ran into the same issue and the problem was that my command class wasn't called exactly Command
, as the docs says. Example:
我遇到了同样的问题,问题是我的命令类没有像文件那样被称为Command。例:
class Command(NoArgsCommand):
# Do something here