Python“来自[dot]包导入...”句法 [英] Python "from [dot]package import ..." syntax

查看:72
本文介绍了Python“来自[dot]包导入...”句法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Looking through a Django tutorial I saw the following syntax:

from .models import Recipe, Ingredient, Instruction

有人可以解释.models的工作方式/它的作用是什么吗?通常我有:

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

from myapp.models import

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

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

推荐答案

可能重复:什么是。在Python中的import语句中是什么意思?

是一个快捷方式,告诉它​​搜索在 PYTHONPATH 的其余部分之前当前包。因此,如果同名模块 Recipe 存在于 PYTHONPATH 中的其他位置,则不会加载。

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.

这篇关于Python“来自[dot]包导入...”句法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆