views:

232

answers:

5

Hi folks,

I'm just wondering, I often have really long python files and imports tend to stack quite quickly.

PEP8 says that the imports should always be written at the beginning of the file.


Do all the imported libraries get imported when calling a function coded in the file? Or do only the necessary libraries get called?

Does it make sense to worry about this? Is there no reason to import libraries within the functions or classes that need them?

+1  A: 

Does it make sense to worry about this?

No

There no reason to import libraries within the functions or classes that need them. It's just slow because the import statement has to check to see if it's been imported once, and realize that it has been imported.

If you put this in a function that's called frequently, you can waste some time with all the import checking.

S.Lott
A: 

Imports happen when the module that contains the imports gets executed or imported, not when the functions are called.

Ordinarily, I wouldn't worry about it. If you are encountering slowdowns, you might profile to see if your problem is related to this. If it is, you can check to see if your module can divided up into smaller modules.

But if all the files are getting used by the same program, you'll just end up importing everything anyway.

jcdyer
Inaccurate. Imports happen when the `import` statement is executed, regardless of where it's located.
Ignacio Vazquez-Abrams
I stand corrected. I knew that, but didn't express it correctly. Thank you.
jcdyer
+3  A: 

Every time Python hits an import statement, it checks to see if that module has already been imported, and if not, imports it. So the imports at the top of your file will happen as soon as your file is run or imported by another module.

There is some overhead to this, so it's generally best to keep imports at the top of your file so that cost gets taken care of up front.

Callahad
+2  A: 

The best place for imports is at the top of your file. That documents the dependencies in one place and makes errors from their absence appear earlier. The import itself actually occurs at the time of the import statement, but this seldom matters much.

It is not typical that you have anything to gain by not importing a library until you are in a function or method that needs it. (There is never anything to gain by doing so inside the body of a class.) It is rare that you want optional dependencies and even rarer that this is the right technique to get them, though. Perhaps you can share a compelling use case?

Mike Graham
I have a class that acts as a generic interface to two plotting tools: gnuplot, which only requires that a file be written and the command be called; and `matplotlib`, which I only use if I want to display the plot in an interactive session. It makes more sense to `print "loading matplotlib"` and `import matplotlib` only in those times when I need it, and not at the beginning of the module (which makes it appear to the user that the script hangs every time this adaptive plotting module is used).
Seth Johnson
@Seth Johnson, Indeed, one valid reason to import within a function is to push of the cost of an expensive first import until it's needed. (I noted this in another comment.) This technique is seldom helpful, though it is on occasion. In many applications, paying a potentially-unnecessary cost upfront ends up making an application *more* responsive, but when a process is launched each time you need it, it can have the opposite effect.
Mike Graham
@Mike: what about a web application, where libraries are loaded for each request?
RadiantHex
@RadiantHex, That use case does not sound especially compelling to me. Perhaps I don't understand what you are doing.
Mike Graham
A: 

If a function inside a module is the only one to import a given other module (say you have a function sending tweets, only if some configuration option is on), then it makes sense to import that specific module in the function.

Unless I see some profiling data proving otherwise, my guess is that the overhead of an import statement in a function is completely negligible.

Olivier
Try it yourself: http://paste.pocoo.org/show/191591/
Callahad
Just because a function is the only one to need a given dependency, that does not mean it makes sense for it to import it itself. Importing at the top of the file has the advantage of documenting dependencies, being able to use the module in additional code, and conforming to the expectations of other programmers. Importing within a function really has two potential purposes: optional dependencies, which is a corner case (and usually implemented with more to it than this) and pushing off the arbitrary cost of *first* import of a module until it's needed, which is even more of a corner case.
Mike Graham
@Callahad: fair enough, at least you give some data! but what does that mean? That an "empty" import takes twice as much time as a call to `string.lower`. That's what I would call negligible.
Olivier
@Olivier. Costs can add up. If you're "negligible" is part of an O( n\*\*2 ) loop, then you're going to see serious performance degradation.
S.Lott