简体   繁体   English

python 中的导入是什么意思?

[英]what does import in python mean?

For example:例如:

if I write如果我写

import math

I am not good in programming.我不擅长编程。 For a program to run it has to load the code to its memory and convert to 0s and 1s which a computer can understand?要运行程序,它必须将代码加载到其 memory 并转换为计算机可以理解的 0 和 1? So, will it load the entire math module when a reference to any function in that module is made in my program, or will it expand the module only once in the program?那么,当在我的程序中对该模块中的任何 function 进行引用时,它会加载整个数学模块,还是只会在程序中扩展该模块一次? If it expands only once, I assume, the computer will load entire python file and all the modules it imports completely in memory?如果它只扩展一次,我假设计算机将加载整个 python 文件以及它在 memory 中完全导入的所有模块? won't that cause a memory running out of space issue if I import too many native python code from the library?如果我从库中导入太多本机 python 代码,这不会导致 memory 空间不足问题吗? Is that the reason some people say it is always good to import exact function in your program instead of wild cards?这就是为什么有人说在你的程序中导入精确的 function 而不是通配符总是好的原因吗?

Will it load the entire math module when a reference to any function in that module is made in my program, or will it expand the module only once in the program?当在我的程序中引用该模块中的任何 function 时,它会加载整个数学模块,还是只会在程序中扩展模块一次?

The math module will be loaded into memory once per Python program (or interpreter).每个 Python 程序(或解释器)将数学模块加载到 memory 一次。

If it expands only once, I assume, the computer will load entire python file and all the modules it imports completely in memory如果它只扩展一次,我假设计算机将加载整个 python 文件以及它在 memory 中完全导入的所有模块

Yes, in normal circumstances.是的,在正常情况下。

Won't that cause a memory running out of space issue if I import too many native python code from the library?如果我从库中导入太多本机 python 代码,这不会导致 memory 空间不足问题吗?

No, not typically.不,通常不会。 Python modules would not put a dent in the memory of modern computers. Python 模块不会影响现代计算机的 memory。

Is that the reason some people say it is always good to import exact function in your program instead of wild cards?这就是为什么有人说在你的程序中导入精确的 function 而不是通配符总是好的原因吗?

No, the entire module will be loaded regardless if you use just one function in it.不,无论您在其中仅使用一个 function,都会加载整个模块。 This is because that one function can rely on any other code in the module.这是因为一个 function 可以依赖模块中的任何其他代码。

The reason it is advised to import specific functions is more of a best practice or recommendation to be explicit about what you are using from the module.建议导入特定函数的原因更多是最佳实践或建议明确说明您从模块中使用的内容。

Also, the module may contain function names in it that are the same as ones you define yourself or are even in another imported module so you want to be careful not to import a bunch of names, especially if you are not going to use them.此外,模块中可能包含 function 名称,这些名称与您自己定义的名称相同,或者甚至在另一个导入的模块中,因此您要小心不要导入一堆名称,特别是如果您不打算使用它们。

Importing a python module means to load the namespace of what is available in that python module, into memory.导入 python 模块意味着将 python 模块中可用的名称空间加载到 memory 中。 Specifically, writing "import " tells python to look for a module with that name on your python path (typically a folder), and if it finds such an object, to then run that objects __init__.py file.具体来说,写“import”告诉 python 在 python 路径(通常是文件夹)上查找具有该名称的模块,如果找到这样的 object,然后运行该对象__init__.py init This file is typically blank, meaning that python should simply load what is available in the module by reading through the files, but this file can also be customized.该文件通常为空白,这意味着 python 应该通过读取文件来简单地加载模块中可用的内容,但也可以自定义此文件。

Python automatically tracks what is loaded and doesn't re-load already loaded items. Python 自动跟踪已加载的内容并且不会重新加载已加载的项目。 Hence, writing因此,写作

import time
import itertools
import itertools as it

doesn't reload time if itertools also uses the time module and doesn't reload itertools if you rename it to it .如果itertools也使用time模块,则不会重新加载time ,如果将itertools重命名为它,则不会重新加载it In general, this is done very quickly;一般来说,这很快就完成了。 in fact, if the code is compiled (some modules are installed already compiled) it can be as fast as literally copying the bytes into memory (down to ns of time regardless of module size).事实上,如果代码已编译(某些模块已安装已编译),它可以像字面上将字节复制到 memory 一样快(无论模块大小如何,只需 ns 时间)。 Importing is one of the fastest commands you can do in python and is rarely the source of any speed issues.导入是您可以在 python 中执行的最快命令之一,并且很少会导致任何速度问题。 Copying and loading bytes into ram can be done millions of times a second and costs the computer nothing.每秒可以将字节复制和加载到 ram 中数百万次,并且不会花费计算机任何成本。 It is when the computer has to perform calculations and compute that there is a concern.当计算机必须执行计算和计算时,就会出现问题。

Something like:就像是:

from itertools import combinations

does not load itertools any faster or slower than if you simply loaded the whole module (there are exceptions to this where some modules are so big that they are broken into sub-packages and so loading at the highest level doesn't load anything at all, for example, scipy , you have to specify the sub-package you want to load).与仅加载整个模块相比,加载 itertools 的速度不会更快或更慢(有一些例外情况是,某些模块太大以至于它们被分解为子包,因此在最高级别加载根本不会加载任何东西,例如scipy ,您必须指定要加载的子包)。

The reason it is recommended to not run from itertools import * is because this loads your namespace with dozens of functions which no one can track.建议不要from itertools import *运行的原因是因为这会加载您的命名空间,其中包含许多没有人可以跟踪的函数。 Suddenly, you use the function combinations and no one that reads your code has any idea which module it came from.突然,您使用了 function 组合,没有人阅读您的代码知道它来自哪个模块。 Hence, it is considered bad practice to * import.因此, * 导入被认为是不好的做法。 Ironically, some programming languages like C do nothing by * importing and it really is impossible to track where variables have come from.具有讽刺意味的是,像 C 这样的一些编程语言通过 * 导入什么也不做,而且真的不可能跟踪变量的来源。

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

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