在python中导入模块时会发生什么? [英] What happens when import modules in python?

查看:173
本文介绍了在python中导入模块时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在python中导入模块文件时会发生什么。我的意思是它的过程,换句话说,python会运行或检查什么?!例如 __ init __。py 或sys.modules等等。例如我知道 __ init __。py 是必要的每个包中的文件,我想知道python在导入时间对这些文件有什么作用?
请为我点亮这个。

I want to know really what happens when we import a module file in python.I mean it's process, in other words what things by python will be run or check?! like __init__.py or sys.modules and etc. for example i know __init__.py are necessary files in every package,i want to know python what does with these files on import time? please light this for me.

推荐答案

阅读关于模块的教程部分 import statement imp 模块(特别是示例)以及 __ import __ 内置。这应该会让你走得很远。如果您还想了解更多,我建议您提出一个具体问题,这个问题有点广泛。

Read the tutorial section about modules, the documentation of the import statement, the imp module (particularly the examples) and maybe the docs for the __import__ builtin. That should get you a long way. If you still want to know more, I'd suggest to ask a specific question, this one is a bit on the broad side.

编辑:再次阅读你的问题之后,你的问题有一个特定的部分,关于 __ init __。py 在包中做了什么。它基本上可以为空或包含将在导入该包时执行的初始化代码。有关详细信息,请参阅有关软件包的部分

Edit: After reading your question once more, there is a specific part to your question, about what __init__.py does in packages. It basically can be empty or contain initialization code that will be executed when that package is imported. See the section about packages for details.

__ init __。py 中你也可以设置 __ all __ ,它定义了哪些符号得到从yourpackage import * 执行时导入。这在从包中导入*中有详细解释

In an __init__.py you could also set __all__, which defines what symbols get imported when you do from yourpackage import *. This is explained in detail in importing * from a package.

这篇关于在python中导入模块时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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