Search code examples
pythondjangomodelsimporterror

django: cant import model to a .py on the same path as models


I have read everything on SO and all the django docs looking for a solution to this. My models work absolutely fine but i cant import them to my 'getnews.py' file in the same directory as models.py, i can to the views.py and there is absolutely no circular imports!

models.py:

from django.db import models

import datetime
from django.utils import timezone

class newsurls(models.Model):
    title = models.CharField(max_length=200)
    def __unicode__(self):
        return unicode(self.title)
    pub_date = models.DateTimeField("date published")



class newsblock(models.Model):
    news = models.ForeignKey(newsurls)
    url = models.URLField(max_length=2000)
    def __unicode__(self):
        return unicode(self.url)
    image = models.URLField(max_length=2000)
    favi = models.URLField(max_length=2000)
    bgcolor = models.CharField(max_length=20)
    tcolor = models.CharField(max_length=20)
    genre = models.CharField(max_length=200)

in views.py: (works)

from news.models import newsurls, newsblock 

getnews.py: (dont work)

from news.models import newsurls, newsblock

traceback:

Traceback (most recent call last):
File "/home/skru/newsshuffle/news/getnews.py", line 3, in <module>
from news.models import newsurls, newsblock
ImportError: No module named news.models

views.py:

from django.shortcuts import render_to_response
from news.models import newsurls, newsblock
try:
    import cPickle as pickle
except:
    import pickle
import random

def news(request):
    newsDatabase = open('/home/skru/newsshuffle/news/storyDb.p', 'r+')
    openData = newsDatabase.read()
    dstory = pickle.loads(openData)
    count = dstory['count']['links']
    story = []
    outstorys = []
    keys = dstory.keys()
    for key in keys:
        if key != 'count':
            story.append(dstory[key]['genre'])
            story.append(dstory[key]['title'])
            story.append(dstory[key]['url'])
            story.append(dstory[key]['image'])
            story.append(dstory[key]['bgcolor'])
            story.append(dstory[key]['via'])
            story.append(dstory[key]['tcolor'])
            outstorys.append(story)
            story = []

    random.shuffle(outstorys)


    lists = newsurls.objects.order_by('-pub_date')[:100]
    return render_to_response('news/news.html',
                          {
                          'story_list':outstorys,
                          'count':count,
                          'lists':lists,
                          })

filepath:

 ├── db.sqlite3
 ├── manage.py
 ├── news
 │   ├── static
 │   │   └── news
 │   │       ├── news.css
 │   │       └── ...
 │   ├── templates
 │   │   └── news
 │   │       ├── allnews.html
 │   │       └── ...
 │   ├── __init__.py
 │   ├── admin.py
 │   ├── dd.py
 │   ├── getPrevDate.py
 │   ├── getnews.py
 │   ├── models.py
 │   ├── storyDb.p
 │   ├── tests.py
 │   ├── urls.py
 │   ├── views.py
 └── newsshuffle
     ├── __init__.py
     ├── settings.py
     ├── urls.py
     └── wsgi.py

i have tried every sort of different import 'newsshuffle.news.models' etc.., added the system path to the system path manually as suggested in other feeds please help!!

manage.py:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "newsshuffle.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

added this because most other posts on SO point to setting DJANGO_SETTINGS_MODULE having to be set manually bur as you can see it already is


Solution

  • Given that directory structure, your import should be working.

    How about trying a relative import instead? In both views.py and getnews.py this should work:

    from models import ...