Python ModuleNotFoundError: 没有名为“models”的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45020963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ModuleNotFoundError: No module named 'models'
提问by user1626536
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
我有一个非常简单的 django 应用程序,我试图将它部署到 heroku,但它一直崩溃。在我的本地机器上一切正常,但在 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:
我正在使用默认的 Django 目录结构:
-python-getting-started
-python-入门
--hello
- 你好
---init.py
---初始化.py
---admin.py (this is where the error is)
---admin.py(这是错误所在)
---models.py (this is the file i'm trying to import)
---models.py(这是我试图导入的文件)
---tests.py
---tests.py
---views.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.
它在我的本地机器上工作得很好。我导入错了吗?老实说,我什至不知道从哪里开始。我在 Heroku 上托管的任何其他 Django 项目都没有任何问题,只有这个。
here is the relevant portion of admin.py that is throwing the error:
这是 admin.py 引发错误的相关部分:
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.
编辑:我刚刚意识到这个应用程序在 heroku 上使用 python v3.6,而我一直在本地机器上使用 python 2.7 进行开发。
采纳答案by vishes_shell
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
.
或者最好导入您将使用的模型,因为它不会在视觉上与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
.
您也不需要将括号与class Meta
.