I have a new Python project, with a models.py file that looks like this:
我有一个新的Python项目,其models.py文件如下所示:
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
class Metric(models.Model):
users = models.ManyToManyField(User, through = 'Vote')
name = models.CharField(max_length = 255)
class Vote(models.Model):
metric = models.ForeignKey(Metric, on_delete = models.CASCADE)
user = models.ForeignKey(User, on_delete = models.CASCADE)
rating = models.IntegerField(validators = [MinValueValidator(0), MaxValueValidator(10)])
email = models.EmailField
def __str__(self):
return str(self.rating)
and an admin.py file like this:
和这样的admin.py文件:
from django.contrib import admin
from models import Metric, Vote
admin.site.register(Metric)
admin.site.register(Vote)
When running this with Python 2.7.5, launching the app works fine. When I try to run it using Python 3.5.1, I get the error ImportError: No module named 'models'
, with this backtrace:
使用Python 2.7.5运行时,启动应用程序运行正常。当我尝试使用Python 3.5.1运行它时,我得到错误ImportError:没有名为'models'的模块,带有这个回溯:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103bd2b70>
Traceback (most recent call last):
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/contrib/admin/apps.py", line 23, in ready
self.module.autodiscover()
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/Users/sashacooper/pyenv/lib/python3.5/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/Users/sashacooper/pyenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/sashacooper/Desktop/pogroms/crockerometer2/crockerometer/admin.py", line 2, in <module>
from models import Metric, Vote
I've tried adding from __future__ import absolute_import
to the start of the admin.py file per similarly titled Stack Overflow posts but it didn't change the error. What's causing it?
我已经尝试从__future__ import absolute_import添加到admin.py文件的开头,每个类似标题的Stack Overflow帖子但它没有改变错误。是什么造成的?
3 个解决方案
#1
8
In Python 3 you must tell it when you are using relative imports:
在Python 3中,您必须在使用相对导入时告诉它:
from .models import ...
#2
2
try from .models import Metric, Vote
in your admin.py
尝试从.models导入Metric,在admin.py中投票
#3
0
I have always used:
我一直用:
from myapp.models import ...
Rather than a relative import. Just my personal preference. Perhaps relative import makes more sense if it is the admin.py
for myapp
, but I still like the explicitness of having the app name in the import.
而不是相对导入。只是我个人的偏好。如果它是myapp的admin.py,也许相对导入更有意义,但我仍然喜欢在导入中使用应用程序名称的明确性。
#1
8
In Python 3 you must tell it when you are using relative imports:
在Python 3中,您必须在使用相对导入时告诉它:
from .models import ...
#2
2
try from .models import Metric, Vote
in your admin.py
尝试从.models导入Metric,在admin.py中投票
#3
0
I have always used:
我一直用:
from myapp.models import ...
Rather than a relative import. Just my personal preference. Perhaps relative import makes more sense if it is the admin.py
for myapp
, but I still like the explicitness of having the app name in the import.
而不是相对导入。只是我个人的偏好。如果它是myapp的admin.py,也许相对导入更有意义,但我仍然喜欢在导入中使用应用程序名称的明确性。