【问题标题】:from django.core.management.base import NoArgsCommand, CommandError ImportError: cannot import name 'NoArgsCommand'从 django.core.management.base 导入 NoArgsCommand,CommandError ImportError:无法导入名称“NoArgsCommand”
【发布时间】:2017-08-23 18:35:21
【问题描述】:
Traceback (most recent call last):
File "prototypes.py", line 39, in <module>
execute_from_command_line(sys.argv)
File "/home/leo/Desktop/learning_log/ll_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/leo/Desktop/learning_log/ll_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/leo/Desktop/learning_log/ll_env/lib/python3.5/site-package/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/leo/Desktop/learning_log/ll_env/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/leo/Desktop/prototypes/sitebuilder/management/commands/build.py", line 42, in handle
call_command('compress',interactive=False,force=True)
File "/home/leo/Desktop/learning_log/ll_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 113, in call_command
command = load_command_class(app_name, command_name)
File "/home/leo/Desktop/learning_log/ll_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 40, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/leo/Desktop/learning_log/ll_env/lib/python3.5/site-packages/compressor/management/commands/compress.py", line 9, in <module>
from django.core.management.base import NoArgsCommand, CommandError
ImportError: cannot import name 'NoArgsCommand'

当我运行 python3 prototypes.py build 时,我遇到了这个问题。我只想压缩css和js文件。这是我的设置。

import os
import sys
from django.conf import settings


BASE_DIR = os.path.dirname(__file__)

settings.configure(
   DEBUG=True,
   SECRET_KEY='plasq@qb+&t-@=x56@=ss=+y-4kp*hj1wy5p!+$cinzhnd+erb',
   ROOT_URLCONF='sitebuilder.urls',
   MIDDLEWARE_CLASSES=(),
   INSTALLED_APPS=(
       'django.contrib.staticfiles',
       'sitebuilder',
       'compressor',
    ),

TEMPLATES=(
    {
        'BACKEND':'django.template.backends.django.DjangoTemplates',
        'DIRS':[],
        'APP_DIRS':True,
    },
),
STATIC_URL='/static/',
SITE_PAGES_DIRECTORY=os.path.join( BASE_DIR,'pages'),
SITE_OUTPUT_DIRECTORY=os.path.join( BASE_DIR,'_build'),

STATIC_ROOT=os.path.join( BASE_DIR,'_build','static'),
STATICFILES_FINDERS=(
                     'django.contrib.staticfiles.finders.FileSystemFinder',
                     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
                     'compressor.finders.CompressorFinder',
                     )
    )
if __name__=="__main__":
     from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

我跟着《轻量级 Django》这本书。

【问题讨论】:

  • 这个问题可以改进。请围绕您遇到的问题提供一些上下文,并总结您的代码打算做什么。此外,非常重要的是您将自己的研究纳入问题,包括您认为可能是问题的原因以及您为尝试修复而采取的步骤。 stackoverflow.com/help/how-to-ask

标签: python django


【解决方案1】:

根据Django documentationNoArgsCommand 类现已弃用,将从 Django 1.10 中删除。请改用BaseCommand,默认情况下不接受任何参数。

这是你可以做的(我注释掉了旧方法供你参考):

#from django.core.management.base import NoArgsCommand
from django.core.management.base import BaseCommand
    
    #class Command(NoArgsCommand):
    class Command(BaseCommand):
    
        #def handle_noargs(self, **options):
        def handle(self, *args, **options):
        ...

【讨论】:

    【解决方案2】:

    试试这个,它在我的情况下工作。

    try:
        from django.core.management.base import NoArgsCommand as BaseCommand
    except ImportError:
        from django.core.management.base import BaseCommand
    
    
    class Command(BaseCommand):
        """ 
        ABC
        def
        """
    

    然后用你自己的实现编写你的类定义。

    【讨论】:

      【解决方案3】:

      试试这个,它在我的情况下工作。

      pip install -U django-extensions 
      

      NoArgsCommand 已被弃用。只需更新库

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        • 2016-01-16
        • 2021-01-12
        • 1970-01-01
        • 2022-12-09
        • 2022-01-22
        相关资源
        最近更新 更多