Python“从[点]包导入...”语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22511792/
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
Python "from [dot]package import ..." syntax
提问by wobbily_col
Looking through a Django tutorial I saw the following syntax:
查看 Django 教程,我看到了以下语法:
from .models import Recipe, Ingredient, Instruction
Can someone explain how the .models works / what it does exactly? Usually I have:
有人可以解释 .models 是如何工作的/它究竟是做什么的?通常我有:
from myapp.models import
How does it work without the myapp part in front of .models?
如果没有 .models 前面的 myapp 部分,它如何工作?
采纳答案by Sudeep Juvekar
Possible duplicate: What does a . in an import statement in Python mean?
可能的重复:. 在 Python 中的 import 语句中是什么意思?
The .is a shortcut that tells it search in currentpackage before rest of the PYTHONPATH. So, if a same-named module Recipeexists somewhere else in your PYTHONPATH, it won't be loaded.
该.条路是捷径,告诉它在搜索当前包的其余部分之前PYTHONPATH。因此,如果同名模块Recipe存在于您的其他位置PYTHONPATH,则不会加载它。
回答by Zulu
In addition of Sudeep Juvekar, this question is also related to manage.py's behavior.
除了Sudeep Juvekar,这个问题也和manage.py他的行为有关。
In django-admin.py and manage.py:
在django-admin.py 和 manage.py 中:
It puts your project's package on
sys.path.
它会将您的项目包放在
sys.path.

