Python “from [dot]package import …” syntax [duplic

2019-01-21 06:45发布

This question already has an answer here:

Looking through a Django tutorial I saw the following syntax:

from .models import Recipe, Ingredient, Instruction

Can someone explain how the .models works / what it does exactly? Usually I have:

from myapp.models import

How does it work without the myapp part in front of .models?

标签: python import
2条回答
smile是对你的礼貌
2楼-- · 2019-01-21 06:54

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-21 07:01

Possible duplicate: What does a . in an import statement in Python mean?

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

查看更多
登录 后发表回答