Search code examples
pythondjangoheroku

ModuleNotFoundError: No module named 'models'


I have a very simple django app that I am attempting to deploy to heroku, but it keeps crashing. Everything works fine on my local machine, but not on heroku

here is the error I am getting (cut to the relevant parts):

File "/app/hello/admin.py", line 4, in <module>
2017-07-10T20:12:27.482194+00:00 app[web.1]:     import models
2017-07-10T20:12:27.482195+00:00 app[web.1]: ModuleNotFoundError: No module 
named 'models'

I am using the default Django directory structure:

-python-getting-started

--hello

---init.py

---admin.py (this is where the error is)

---models.py (this is the file i'm trying to import)

---tests.py

---views.py

It works just fine on my local machine. Am I importing it wrong? I honestly don't even know where to start with this. I do not have any problems on any of my other Django projects hosted on Heroku, just this one.

here is the relevant portion of admin.py that is throwing the error:

from django.contrib import admin
from django import forms

import models

# Register your models here.
class BasicInfoCollectionForm(forms.ModelForm):
    class Meta():
        model = models.VolunteerBasicInfo
        fields = ('removed for brevity')

Any help would be greatly appreciated

edit: I just realized that this app is using python v3.6 on heroku, while i've been doing dev with python 2.7 on my local machine.


Solution

  • You need to use relative import

    from . import models
    

    Or it's better to import models that you will use, since it won't visually collide with django.db.models.

    from django import forms
    
    from .models import VolunteerBasicInfo
    
    class BasicInfoCollectionForm(forms.ModelForm):
        class Meta:
            model = VolunteerBasicInfo
            ...
    

    You also don't need to use brackets with class Meta.