简体   繁体   English

Python如何在内部区分“来自模块导入功能”和“来自模块导入功能”

[英]How does Python internally distinguish “from package import module” between “from module import function”

If I understand correctly, the python syntax from ... import ... can be used in two ways 如果我正确理解,则可以通过两种方式使用from ... import ...的python语法

  1. from package-name import module-name 从软件包名称导入模块名称
  2. from module-name import function-name 从模块名称导入功能名称

I would like to know a bit of how Python internally treats the two different forms. 我想了解一下Python在内部如何处理两种不同形式。 Imagine, for example, that the interpreter gets "from A import B", does the interpreter actually try to determine whether A is a package-name/ module-name, or does it internally treat packages and modules as the same class of objects (something like Linux treats files and directories very similarly)? 例如,想象一下,解释器“从A导入B获取”,解释器实际上是在尝试确定A是软件包名称/模块名称,还是在内部将软件包和模块视为同一类对象(像Linux之类的文件和目录处理方式非常相似)?

First of all, a module is a python file that contains classes and functions. 首先,模块是一个包含类和函数的python文件。 when you say From A Import B python searches for A(a module) in the standard python library and then imports B(the function or class) which is the module if it finds A. If it doesn't it goes out and starts searching in the directory were packages are stored and searches for the package name( A ) and then if it finds it, it imports the Module name(B). 当您说从导入B时,python在标准python库中搜索A(一个模块),然后导入B(函数或类)(如果找到A,则为模块)。否则,它熄灭并开始搜索在目录中,存储软件包并搜索软件包名称(A),然后找到它,则导入模块名称(B)。 If it fails in the past 2 processes it returns an error. 如果它在过去2个进程中失败,则返回错误。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM