2
votes

I'm building a Django app and am in the process of migrating to Heroku and Heroku Postgres. When I run "python manage.py runserver" or even "heroku run python manage.py runserver" Terminal returns the error "Error: No module named filename" - with the filename being the app folder sitting inside my main project folder that contains my models.py and views.py files etc. Please see the file structure below.

I have the app folder listed in my INSTALLED APPS and it shows up when I run "ls" for projectname so I'm puzzled as to why it can't be found.

I have started a new app using startapp and moved the contents of the old one over just to see if it was an anomaly. This only seems to have happened since trying to get Heroku working.

Here's my file structure for this project:

projectname/
      manage.py
      requirements.txt
      Procfile
      projectname/
           templates/
           __init__.py
           settings.py
           urls.py
           wsgi.py
      filename/
           __init__.py
           admin.py
           models.py
           tests.py
           urls.py
           views.py
      static/
      venv/

I've been struggling with this for over a day now so any help on how to get past it would be much appreciated!

EDIT: Here are all the places where I have mentioned filename in my code:

  • In admin.py as 'from projectname.filename.models import *'
  • In projectname/urls.py under URLconf as include('filename.urls',
    'namespace=filename')
  • In filename/urls.py as 'from filename import *' and under URLconf as 'urlpatterns = patterns('filename.views' ...)
  • In views.py as 'from filename.models import *'

I have run 'heroku run python manage.py syncdb' in venv as well and it still returns the error "Error: No module named filename".

3
Where in your code do you do import filename? is it somewhere under projectname?favoretti
@favoretti I've added an edit to my question above listing where I import filename across my code. I'm not sure if I have imported filename under projectname. How can I check if I have done so? I hope the above edit helps.Jess

3 Answers

2
votes

I think this line

In admin.py as 'from projectname.filename.models import *'

is giving issues. The projectname part IMHO shouldn't be there, so the line should just read

from filename.models import *

[edit]

And, both projectname and filename should be in the INSTALLED_APPS.

And to explain more on my line of thinking. Your projectname.filename refers to the inner projectname folder, so I'm thinking that it's trying to find projectname/projectname/filename.py file that doesn't exist. Just adding filename to INSTALLED_APPS will make the scope globally available and hence can be referred to as-is.

0
votes

I have a routine I use to dump the entirety of the Django module paths. The re.sub()s are there just to shorten up some of the paths and make it easier to see what is where.

import sys, re, os

def ModuleList():
    ret = []
    dir_project = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    project_name = os.path.basename(dir_project)

    for k,v in sys.modules.items():

        x = str(v)
        if 'built-in' in x:
            ret.append((k, 'built-in'))
            continue

        m = re.search(r"^.*?'(?P<module>.*?)' from '(?P<file>.*?)'.*$", x)
        if m:
            d = m.groupdict()
            f = d['file']
            f = re.sub(r'/usr/local/lib/python[.0-9]*/site-packages/django/', 'system django >> ', f)
            f = re.sub(r'/usr/local/lib/python[.0-9]*/site-packages/', 'site-packages >> ', f)
            f = re.sub(r'/usr/local/lib/python[.0-9]*/', 'python >> ', f)
            f = re.sub(dir_project+'.*django/', 'local django >> ', f)
            f = re.sub(dir_project+r'(/\.\./)?', project_name + ' >> ', f)
            ret.append((d['module'], f))
    ret.sort( lambda a,b: cmp(a[0].lower(), b[0].lower()) )
    return ret
# ModuleList

if __name__ == "__main__":
    for x in ModuleList():
        print "%s\t%s" % (x[0], x[1])
0
votes

Are you in /venv? I've run into similar errors after forgetting to enter the virtual environment. To do so from the terminal, try:

$ source venv/bin/activate