Python 在 Django 中从 py3 到 py2 不再识别带有前导点的导入
Python imports with leading dot no longer recognised going from py3 to py2 in django
我已将使用 python3 进行的项目复制到只有 python2 的工作站。 Python 现在正在抱怨我的一件进口商品。
该行是
from .mage.models import ButtLoadOfClasses
但是错误说
ImportError: No module named mage.models
我的项目树是这样的:
nwod-db-master/
characters
mage
models.py
admin.py
导入在 admin.py。
所以我认为它正在切掉点,然后将其视为绝对导入 - mage.models 不在我的路径上,所以它很讨厌。
我搜索过这个,我能找到的最相关的东西在这里 What's wrong with relative imports in Python? which is more about coding style, and doesn't lead me to a solution - except that it leads me to believe that relative imports are a python3 only thing. I also saw this: Python/Django is importing the wrong module (relative when it should be absolute) 但那是针对模板标签的。我还没有在接受的答案中求助于 hackery,因为我希望事情在过去 ~4 年里有所进展。
如何强制 python 将其作为包的绝对引用导入?
您似乎没有在 mage/ 中包含 __init__.py
文件,因此无法将其识别为包。
只需在 mage/
下添加一个 __init__.py
文件
nwod-db-master/
characters/
mage/
__init__.py
models.py
admin.py
我已将使用 python3 进行的项目复制到只有 python2 的工作站。 Python 现在正在抱怨我的一件进口商品。
该行是
from .mage.models import ButtLoadOfClasses
但是错误说
ImportError: No module named mage.models
我的项目树是这样的:
nwod-db-master/
characters
mage
models.py
admin.py
导入在 admin.py。
所以我认为它正在切掉点,然后将其视为绝对导入 - mage.models 不在我的路径上,所以它很讨厌。
我搜索过这个,我能找到的最相关的东西在这里 What's wrong with relative imports in Python? which is more about coding style, and doesn't lead me to a solution - except that it leads me to believe that relative imports are a python3 only thing. I also saw this: Python/Django is importing the wrong module (relative when it should be absolute) 但那是针对模板标签的。我还没有在接受的答案中求助于 hackery,因为我希望事情在过去 ~4 年里有所进展。
如何强制 python 将其作为包的绝对引用导入?
您似乎没有在 mage/ 中包含 __init__.py
文件,因此无法将其识别为包。
只需在 mage/
下添加一个__init__.py
文件
nwod-db-master/
characters/
mage/
__init__.py
models.py
admin.py