Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63739 > unrolled thread

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

Started bySam <lightaiyee@gmail.com>
First post2014-01-11 17:28 -0800
Last post2014-01-11 20:49 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Is it better to import python modules inside function or at the top? What are the pros and cons? Sam <lightaiyee@gmail.com> - 2014-01-11 17:28 -0800
    Re: Is it better to import python modules inside function or at the top? What are the pros and cons? Chris Angelico <rosuav@gmail.com> - 2014-01-12 12:48 +1100
    Re: Is it better to import python modules inside function or at the top? What are the pros and cons? Ned Batchelder <ned@nedbatchelder.com> - 2014-01-11 20:49 -0500

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

FromSam <lightaiyee@gmail.com>
Date2014-01-11 17:28 -0800
SubjectIs it better to import python modules inside function or at the top? What are the pros and cons?
Message-ID<ccb82f25-c007-4e90-97d9-5108463ed51f@googlegroups.com>
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?

[toc] | [next] | [standalone]


#63741

FromChris Angelico <rosuav@gmail.com>
Date2014-01-12 12:48 +1100
Message-ID<mailman.5349.1389491293.18130.python-list@python.org>
In reply to#63739
On Sun, Jan 12, 2014 at 12:28 PM, Sam <lightaiyee@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#63742

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-01-11 20:49 -0500
Message-ID<mailman.5350.1389491356.18130.python-list@python.org>
In reply to#63739
On 1/11/14 8:28 PM, Sam wrote:
> 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).

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web