Is it better to import python modules inside function or at the top? What are the pros and cons?

Sam lightaiyee at gmail.com
Sat Jan 11 20:28:18 EST 2014


I have python modules which are used only in specific functions and the functions are not called all the time. Is it better to import the function inside the function only or is it a better practice to always import all modules at the top of the script? If I import the module inside the function, will it cause a big performance hit because of the import module action that gets to be called every time the function is called?

What are the pros and cons of each approach?



More information about the Python-list mailing list