简体   繁体   中英

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? 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? If it expands only once, I assume, the computer will load entire python file and all the modules it imports completely in memory? won't that cause a memory running out of space issue if I import too many native python code from the library? Is that the reason some people say it is always good to import exact function in your program instead of wild cards?

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?

The math module will be loaded into memory once per Python program (or interpreter).

If it expands only once, I assume, the computer will load entire python file and all the modules it imports completely in 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?

No, not typically. Python modules would not put a dent in the memory of modern computers.

Is that the reason some people say it is always good to import exact function in your program instead of wild cards?

No, the entire module will be loaded regardless if you use just one function in it. This is because that one function can rely on any other code in the module.

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.

Importing a python module means to load the namespace of what is available in that python module, into 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. 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 automatically tracks what is loaded and doesn't re-load already loaded items. 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 . 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). Importing is one of the fastest commands you can do in python and is rarely the source of any speed issues. Copying and loading bytes into ram can be done millions of times a second and costs the computer nothing. 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).

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. Suddenly, you use the function combinations and no one that reads your code has any idea which module it came from. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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