Command line script > Ensure that its module, "models", is located inside an installed app. error

591 views
Skip to first unread message

Oscar Buijten

unread,
Jun 7, 2015, 8:26:39 AM6/7/15
to django...@googlegroups.com
Hi There,

For various reasons I recently started efforts to covert an existing .php application to pyhton + django.
There are quite a few scripts that are executed through cron and so from the command line while coding.

I did find some info on how to setup the start of the script, but keep running into an error I haven't been able to resolve so far.

Using Python 2.7.x and django 1.8.x + postresql

Script is starting with this:

----------------------  snip  ----------------------
import os,sys
sys.path.append('/home/oscar/django/trading/myichimoku')
os.environ['DJANGO_SETTINGS_MODULE']='myichimoku.settings'
from models import MyichiTickers
----------------------  snip  ----------------------

The script lives in /home/oscar/django/trading/myichimoku/data in which the models.py lives as well

The error I get:

----------------------  snip  ----------------------
Traceback (most recent call last):
  File "historic_data_collector.py", line 4, in <module>
    from models import MyichiTickers
  File "/home/oscar/django/trading/myichimoku/data/models.py", line 4, in <module>
    class AuthGroup(models.Model):
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 131, in __new__
    'app.' % (new_class.__name__, model_module.__name__)
django.core.exceptions.ImproperlyConfigured: Unable to detect the app label for model "AuthGroup." Ensure that its module, "models", is located inside an installed app.
----------------------  snip  ----------------------

AuthGroup is the 1st table class in models.py

My settings.py lives in: /home/oscar/django/trading/myichimoku/myichimoku/

Here a snippet from the file:
----------------------  snip  ----------------------
# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'data',
)
----------------------  snip  ----------------------


So the question is: how can I ensure to execute this script from the command line with something like: python mycoolscript.py ?

Any help greatly appreciated.

Thanks!
Oscar

Gergely Polonkai

unread,
Jun 7, 2015, 8:44:33 AM6/7/15
to django...@googlegroups.com
Hello,

you should remove the sys.path.append() line, and use from .models import MyichiTickers.

Also, it seems that you are writing a custom python script instead of implementing it in your project. If I were you, I would create a django-admin command, so it would work like

python manage.py historic_data_collector

You can find more info on this at [1]

Best,
Gergely


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e72dbbe3-b6fb-4294-b4bc-04d5881b2f8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oscar Buijten

unread,
Jun 7, 2015, 9:18:27 AM6/7/15
to django...@googlegroups.com
Thanks Gergely! :-)

I followed your suggestions and it does seem to improve behaviour, however, I now get a new error...

----------------------  snip  ----------------------
Traceback (most recent call last):
  File "historic_data_collector.py", line 4, in <module>
    from .models import MyichiTickers
ValueError: Attempted relative import in non-package
----------------------  snip  ----------------------

I've done a quick google on it but didn't see anything that would help me out.
Any suggestions?

Also, I had a look at your suggestion to create a django-admin command last night (but ran into similar issues).
Besides a solution in itself would you mind giving some feedback on why that would be better? I didn't get that (yet).

FYI: there are several scripts running on regular interval updating data in a database that the web application then uses to show to the end user. I now just am converting to python/django

Thanks again,
Oscar

Gergely Polonkai

unread,
Jun 7, 2015, 9:49:55 AM6/7/15
to django...@googlegroups.com
Hello,

Sorry, I forgot that relative imports don’t work in standalone scripts, just modules. In this case, yes, you should go on with the original import line of yours, and modify the loading path. However, if you want to use your models outside of Django, that is 1) pretty much unsupported and 2) you must initialize your app (I hope someone can point to a document that shows how; I have no knowledge on this. I guess you will have to mess around with django.apps).

Well, all this is unless you create a django-admin command, which will do all this for you (the application/model initialization). When you invoke a command like “python manage.py migrate”, all your apps in INSTALLED_APPS get loaded, and thus, initialized. After that, in the command’s handle() method, you can do whatever you want. Also another thing you win is that the maintenance scripts will be actually integrated into your application and you can even write tests for them.

Best,
Gergely

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Oscar Buijten

unread,
Jun 7, 2015, 11:06:48 AM6/7/15
to django...@googlegroups.com
Thanks for your swift responses Gergely. It's much appreciated ;-)
I will try again for the django-admin command
Oscar

Le dimanche 7 juin 2015 14:26:39 UTC+2, Oscar Buijten a écrit :

Oscar Buijten

unread,
Jun 8, 2015, 8:23:07 AM6/8/15
to django...@googlegroups.com
Just to let you know that I got this snippet working now Gergely :-)
Thanks again for putting me on the right track.
What I didn't get though is why I need the " *args, **options". 
I read the doc but didn't grasp the why...
Cheers,
Oscar

----------------------  snip  ----------------------
from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
def handle(self, *args, **options):
from data.models import MyichiTickers
tickers_for_exchange = MyichiTickers.objects.filter(exchange="AMS")
for tickerlist in tickers_for_exchange:
print(tickerlist.ticker)
----------------------  snip  ----------------------


Le dimanche 7 juin 2015 14:26:39 UTC+2, Oscar Buijten a écrit :

felix

unread,
Jun 8, 2015, 8:59:16 AM6/8/15
to django...@googlegroups.com
El 08/06/15 08:23, Oscar Buijten escribió:
Just to let you know that I got this snippet working now Gergely :-)
Thanks again for putting me on the right track.
What I didn't get though is why I need the " *args, **options". 
I read the doc but didn't grasp the why...
Cheers,
Oscar

I'm a newbie. I guess that *args, **options will keep your script working no matter how many positional or keyword arguments your command or the base command may have in the future.

Cheers,
Felix.

Gergely Polonkai

unread,
Jun 8, 2015, 9:48:23 AM6/8/15
to django...@googlegroups.com
Hello,

This SO answer[1] will provide you with decent answer on what this *args and **kwargs things are. Hope that helps!

Best,
Gergely


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages