Python“ from [ dot ] package import...”语法

通过查看 Django 教程,我看到了以下语法:

from .models import Recipe, Ingredient, Instruction

有人能解释一下.model 是如何工作的/它到底是做什么的吗? 通常我有:

from myapp.models import

如果没有 myapp 在.model 前面的部分,它是如何工作的?

68730 次浏览

The . is a shortcut that tells it to search in the current package before the rest of the PYTHONPATH. So, if a same-named module Recipe exists somewhere else in your PYTHONPATH, it won't be loaded.

In addition of Sudeep Juvekar, this question is also related to manage.py's behavior.

In django-admin.py and manage.py:

It puts your project’s package on sys.path.