AttributeError: 'module' object has no attribute 'Command' - python

I'm working with django 1.7 and i have created this management command:
File structure:
app/
__init__.py
management/
__init__.py
commands/
__init__.py
modal_asign.py
Code:
from django.core.management.base import BaseCommand, CommandError
from webinar.models import Webinar
class Commands(BaseCommand):
help = 'Assign modal link to every webinar'
def handle(self):
latest_webinar = Webinar.objects.all().order_by('-webinar_date')[0]
for webinars in latest_webinar:
print "http://webinar.academiaa2.com/webinars/#displayModal" + webinars
and i'm getting this error when trying to test it with python manage.py modal_asign
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/rafael/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/rafael/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/rafael/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 238, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/rafael/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 42, in load_command_class
return module.Command()
AttributeError: 'module' object has no attribute 'Command'

Your class name is called Commands, whereas django expects it to be called Command. Hence the error.
Change
class Commands(BaseCommand):
to
class Command(BaseCommand):

Related

AttributeError: 'module' object has no attribute 'commit_on_success'

I'm trying to use Django-nested-inlines application but it raises error. Here is a simple code:
MODELS.PY
class Language_Quiz(models.Model):
name = models.CharField(max_length=40)
language = models.OneToOneField(sfl_models.Language)
class Question(models.Model):
language_quiz = models.ForeignKey(Language_Quiz)
text = models.TextField()
class Answer(models.Model):
question = models.ForeignKey(Question,related_name='answers')
text = models.TextField()
correct = models.BooleanField()
ADMIN.PY
from django.contrib import admin
import models
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline
class AnswerInline(NestedTabularInline):
model = models.Answer
class QuestionInline(NestedTabularInline):
model = models.Question
class LanguageQuizAdmin(NestedModelAdmin):
inlines = [QuestionInline]
admin.site.register(models.Language_Quiz,LanguageQuizAdmin)
admin.site.register(models.Answer)
admin.site.register(models.Question)
But when I run server or try to make migrations, the error is raised. I have no idea where is the problem. Where is the problem?
TRACEBACK
(venv) C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line
utility.execute()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\__init__.py", line 328, in execute
django.setup()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\apps\registry.py", line 115, in populate
app_config.ready()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\contrib\admin\apps.py", line 22, in ready
self.module.autodiscover()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\utils\module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\language_tests\admin.py", line 3, in <module>
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\nested_inlines\admin.py", line 16, in <module>
class NestedModelAdmin(ModelAdmin):
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\nested_inlines\admin.py", line 131, in NestedModelAdmin
#transaction.commit_on_success
AttributeError: 'module' object has no attribute 'commit_on_success'
As Kapil Sachdev and karthikr commented, the problem is with compatibility. Django-nested-inlines has probably some issues working with Django 1.8+.
In my case, the solution was very simple. Download using pip Django-nested-inline (without 's' at the end of the 'inline') and just change import. I didn't even have to change the code.

ImportError: No module named simple while running manage.py test in django project

Traceback:
./manage.py test my_app
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/zuber/projects/private-CR/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/zuber/projects/private-CR/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/zuber/projects/private-CR/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/zuber/projects/private-CR/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 381, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "/home/zuber/projects/private-CR/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 354, in create_parser
self.add_arguments(parser)
File "/home/zuber/projects/private-CR/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 52, in add_arguments
test_runner_class = get_runner(settings, self.test_runner)
File "/home/zuber/projects/private-CR/env/local/lib/python2.7/site-packages/django/test/utils.py", line 152, in get_runner
test_module = __import__(test_module_name, {}, {}, force_str(test_path[-1]))
ImportError: No module named simple
I tried to remove init.py from app folder and then I've got "No module named app_name". When I removed init.py from project folder - console said "No module named settings". How to test my app?
So first of all, you need to put your two __init__.pys back where you found them. They are what allow you to import things from that module. Secondly, you should post the code in manage.py so we have a better idea of what is going on, but it looks to me like you had a line in there that looks something like import django.contrib.admin.util or import <something> from django.contrib.admin.util. This module was removed in the release of django that you're using, so you should replace any occurrances of django.contrib.admin.util with django.contrib.admin.utils.
I resolved the problem by removing
TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'
from settings.py
see related question running all tests post django 1.6
For me while using Pycharm the problem was that the PyCharm test runner was not compatible with Django 2.0
The solution was:
replace line 254:EOF with
if VERSION[1] > 1 or VERSION[0] > 1:
return DjangoTeamcityTestRunner(**options).run_tests(test_labels,
extra_tests=extra_tests, **options)
return run_the_old_way(extra_tests, options, test_labels, verbosity)
Instead of:
if VERSION[1] > 1:
return DjangoTeamcityTestRunner(**options).run_tests(test_labels,
extra_tests=extra_tests, **options)
return run_the_old_way(extra_tests, options, test_labels, verbosity)

How to Import Function from different directory in python

I have following file structure:
/usr/share/pyshared/orca/scripts/toolkits/Gecko/script.py
In this file def getExtents is function that I have to access in following file i.e:
/usr/share/pyshared/orca/scripts/default.py
so I am trying to import function from script.py to the following file:
/usr/share/pyshared/orca/scripts/default.py
following line is added in /usr/share/pyshared/orca/scripts/default.py:
from orca.scripts.toolkits.Gecko.script import getExtents
But I get following errors:
File "<string>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/orca/orca.py", line 1498, in main
init(pyatspi.Registry)
File "/usr/lib/pymodules/python2.7/orca/orca.py", line 1215, in init
loadUserSettings()
File "/usr/lib/pymodules/python2.7/orca/orca.py", line 837, in loadUserSettings
_scriptManager.activate()
File "/usr/lib/pymodules/python2.7/orca/script_manager.py", line 80, in activate
self.setActiveScript(self.getScript(None), "activate")
File "/usr/lib/pymodules/python2.7/orca/script_manager.py", line 247, in getScript
appScript = self.getDefaultScript()
File "/usr/lib/pymodules/python2.7/orca/script_manager.py", line 214, in getDefaultScript
import scripts.default as default
File "/usr/lib/pymodules/python2.7/orca/scripts/default.py", line 57, in <module>
from orca.scripts.toolkits.Gecko.script import getExtents
File "/usr/lib/pymodules/python2.7/orca/scripts/toolkits/Gecko/__init__.py", line 1, in <module>
from script import Script
File "/usr/lib/pymodules/python2.7/orca/scripts/toolkits/Gecko/script.py", line 52, in <module>
import orca.scripts.default as default
AttributeError: 'module' object has no attribute 'default'
I think you probably need
from orca.scripts import default
Also, as mentioned in the comments, check you've got the relevant __init__.py
edit: re-reading the stacktrace, looks like you've got a circular import too, you're trying to import orca.scripts.default in line 52 of "/usr/lib/pymodules/python2.7/orca/scripts/toolkits/Gecko/script.py"

ImportError: cannot import name normalize

I tried to use djangos i18n and therefor had to install gettext. Now I'm getting this awkward error any time I try to start my application:
Traceback (most recent call last):
File "/var/www/mydjangoproject/manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/core/management/__init__.py", line 4, in <module>
from optparse import OptionParser, NO_DEFAULT
File "/usr/lib/python2.7/optparse.py", line 418, in <module>
_builtin_cvt = { "int" : (_parse_int, _("integer")),
File "/usr/lib/python2.7/gettext.py", line 581, in gettext
return dgettext(_current_domain, message)
File "/usr/lib/python2.7/gettext.py", line 545, in dgettext
codeset=_localecodesets.get(domain))
File "/usr/lib/python2.7/gettext.py", line 480, in translation
mofiles = find(domain, localedir, languages, all=1)
File "/usr/lib/python2.7/gettext.py", line 437, in find
for nelang in _expand_lang(lang):
File "/usr/lib/python2.7/gettext.py", line 131, in _expand_lang
from locale import normalize
ImportError: cannot import name normalize
The library locale.py is present and I can import it. Django 1.4 is running under Python 2.7.2+. Any Ideas?
I guess you have somewhere inside your project a folder locale with an __init__.py file in it.
Delete this file (you don't need it for translation to work) and you should be good to go.

Django FeinCMS and page extensions issue

I'm experimenting with Page extensions and FeinCMS 1.2.1.
I'd like to create this simple page extension:
from django.db import models
def register(cls, admin_cls):
cls.add_to_class(models.TextField())
but I keep getting this error:
AttributeError: 'module' object has no attribute 'TextField'
The package containing the extension is called dev.extensions and the module categories, I have added it to the Python path and the extension is registered with Page.register_extensions('dev.extensions.categories').
I'm wondering what I'm doing wrong, please help.
The complete stacktrace follows.
c:\sandbox\projects\feindev>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 13, in <module>
execute_manager(settings)
File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\django\core\management\__init__.py", line 436, in execute_m
anager
setup_environ(settings_mod)
File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\django\core\management\__init__.py", line 419, in setup_env
iron
project_module = import_module(project_name)
File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "c:\sandbox\projects\feindev\..\fein
dev\__init__.py", line 52, in <module>
Page.register_extensions('dev.extensions.types')
File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\feincms\models.py", line 270, in register_extensions
cls.register_extension(fn)
File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\feincms\module\page\models.py", line 564, in register_exten
sion
register_fn(cls, PageAdmin)
File "c:\sandbox\projects\feindev\dev\ext
ensions\types\__init__.py", line 4, in register
cls.add_to_class(models.TextField())
AttributeError: 'module' object has no attribute 'TextField'
Maybe models is getting reassigned somewhere to another module before your register function gets run?
Try changing your code to this:
from django.db import models as django_models
def register(cls, admin_cls):
cls.add_to_class('field_name_here', django_models.TextField())

Categories