0

I want to add the models to the commands. /home/max/askmoiseev/ask/management/commands/cron.py

# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand, CommandError
from ask.models import Tag


class Command(BaseCommand):

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

$ python manage.py cron

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 75, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
  File "/home/max/askmoiseev/ask/management/commands/cron.py", line 4, in <module>
from ask.models import Tag
  File "/home/max/askmoiseev/ask/models.py", line 3, in <module>
from loginsys.models import User
  File "/home/max/askmoiseev/loginsys/__init__.py", line 3, in <module>
from ask.models import User
ImportError: cannot import name User

What's the problem? I tried to do so:

#!/bin/bash
export DJANGO_SETTINGS_MODULE=askmoiseev.settings
./manage.py cron

But it did not help.

russianstudent
  • 129
  • 2
  • 12

1 Answers1

2

The below should work. You must make sure the Python/Django package askmoiseev is importable. To make it importable the simplest way is to set your current working directory as the directory on which the package askmoiseev exists.

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'askmoiseev.settings'
from ask.models import Tag
rogue-one
  • 11,259
  • 7
  • 53
  • 75
  • can make sure your current working directory is in the root of your Django project? – rogue-one Jun 01 '14 at 09:52
  • **max@Q210:~/askmoiseev$ ls** ask askmoiseev cron.sh loginsys manage.py scripts static templates uploads **max@Q210:~/askmoiseev$ python manage.py cron** Traceback (most recent call last): File "manage.py", line 10, in execute_from_command_line(sys.argv)......... – russianstudent Jun 01 '14 at 09:57
  • run "from askmoiseev import settings" from your cwd. if it doesn't work then your problem is that the package askmoiseev is not visible to Python. – rogue-one Jun 01 '14 at 10:03
  • **max@Q210:~/askmoiseev$ from askmoiseev import settings** from: can't read /var/mail/askmoiseev – russianstudent Jun 01 '14 at 10:09
  • @russianstudent can you run pwd? – rogue-one Jun 01 '14 at 10:12
  • **max@Q210:~/askmoiseev$ pwd** /home/max/askmoiseev – russianstudent Jun 01 '14 at 10:14
  • @russianstudent "from askmoiseev import settings" this should be run from Python console. You ran it from the Bash shell. – rogue-one Jun 01 '14 at 10:19
  • all ok, **max@Q210:~/askmoiseev$ python** Python 2.7.3 (default, Feb 27 2014, 20:00:17) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> **from askmoiseev import settings** >>> – russianstudent Jun 01 '14 at 10:21
  • I do not know why, but now the error is not present, thanks for the help – russianstudent Jun 01 '14 at 12:13