Python“从[点]包导入..."句法 [英] Python "from [dot]package import ..." syntax

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

问题描述

查看 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

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

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

推荐答案

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

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

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.

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

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