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

S

Sam

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?
 
C

Chris Angelico

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?

Most of the work of importing is still going to be done only once
(after that, the import just grabs a cached reference to the same
module). It's normally clearer to import at the top of the module,
especially if you have multiple functions calling on the same module,
but there are two other concerns:

1) If the module can't be found, would you rather know as soon as your
module is loaded, or is it better to load your module without it and
then fail only when that function is called?

2) If the module takes a long time to load, would you rather pay that
cost as soon as your module is loaded, or on the first use of the
function that needs it?

ChrisA
 
N

Ned Batchelder

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?

Unless there's a good reason, you should import all the modules at the
top of the file.

Reasons to import in a function:

1) if a function is only sometimes called, and the import is expensive.

2) if a function is only sometimes called, and needs a dependency that
not all users of your package need. For example, your library has both
Flask and Django helper functions, and only Django users call the Django
function, etc.

3) if you have a circular import, though that can often be fixed in
better ways.

Note that the cost of imports is only incurred at the first import, so
you don't have to worry about the import statement executing each time
your function is called. After the first import, the cost is about the
same as a dict lookup (very fast).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top