13

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.

user1626536
  • 793
  • 1
  • 6
  • 14

2 Answers2

25

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.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
vishes_shell
  • 22,409
  • 6
  • 71
  • 81
  • I would suggest absolute imports as py3 get's rid of relative. – Adam Jul 10 '17 at 20:35
  • yeah, this solved the problem. It turns out that heroku changed the default version of their django app to python 3 and I didn't notice for this project. Thank you! – user1626536 Jul 10 '17 at 20:37
  • @Adam Python 3 will remove relative imports, can you give me a link of proof, please? – vishes_shell Jul 10 '17 at 20:40
  • I misread that you can use explicit relative still. I always go w/ absolute my bad – Adam Jul 10 '17 at 20:44
  • OP can you mark the answer correct for @vishes_shell – Adam Jul 10 '17 at 20:46
  • @Adam yeah, be careful. Just another proof, python 3.6 [intra-package-references](https://docs.python.org/3/tutorial/modules.html#intra-package-references). :) – vishes_shell Jul 10 '17 at 20:46
  • Yeah I do a mix of 2.7 (legacy apps) & 3.6 (new apps) at work and it is standard to use `from __future__ import absolute_import` for all our older apps that haven't switched so I thought you could only do absolute. I believe absolute is always preferred. https://stackoverflow.com/a/4209771/3990806 – Adam Jul 10 '17 at 20:53
  • @Adam look at the top voted comment by Brandon Rhodes: "that part of PEP-8 is outdated, according to Guido. mail.python.org/pipermail/python-dev/2010-October/104476.htm‌​l " And in large applications its better to use relative imports. Even django uses it, e.g. [django.core.management.utils](https://github.com/django/django/blob/master/django/core/management/utils.py) – vishes_shell Jul 10 '17 at 21:06
0

Thanks i had this error because i forgot the dot

project/base/admin.py

from .models import Tarea

(Before i texted from models import Tarea

  • Hello, and welcome to Stack Overflow. This type of commentary is better provided as a comment on the original question or answer, rather than as an answer itself. – sloppypasta Mar 01 '23 at 21:41