Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import statement inside class/function definition - is it a good idea?

People also ask

Is it a good practice to import inside function in Python?

Importing inside a function will effectively import the module once.. the first time the function is run. It ought to import just as fast whether you import it at the top, or when the function is run. This isn't generally a good reason to import in a def.

Why is the use of import all statement not recommended?

Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.

What is the advantage of using import statements?

Advantages: Import statements help in reducing the code size and hence save a lot of time. It improves the readability of our code. It is pretty useful while handling big projects.

Where do import statements go in class Python?

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. Of course this is no hard and fast rule, and imports can go anywhere you want them to. But putting them at the top is the best way to go about it.


It's the most common style to put every import at the top of the file. PEP 8 recommends it, which is a good reason to do it to start with. But that's not a whim, it has advantages (although not critical enough to make everything else a crime). It allows finding all imports at a glance, as opposed to looking through the whole file. It also ensures everything is imported before any other code (which may depend on some imports) is executed. NameErrors are usually easy to resolve, but they can be annoying.

There's no (significant) namespace pollution to be avoided by keeping the module in a smaller scope, since all you add is the actual module (no, import * doesn't count and probably shouldn't be used anyway). Inside functions, you'd import again on every call (not really harmful since everything is imported once, but uncalled for).


PEP8, the Python style guide, states that:

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

Of course this is no hard and fast rule, and imports can go anywhere you want them to. But putting them at the top is the best way to go about it. You can of course import within functions or a class.

But note you cannot do this:

def foo():
    from os import *

Because:

SyntaxWarning: import * only allowed at module level

Like flying sheep's answer, I agree that the others are right, but I put imports in other places like in __init__() routines and function calls when I am DEVELOPING code. After my class or function has been tested and proven to work with the import inside of it, I normally give it its own module with the import following PEP8 guidelines. I do this because sometimes I forget to delete imports after refactoring code or removing old code with bad ideas. By keeping the imports inside the class or function under development, I am specifying its dependencies should I want to copy it elsewhere or promote it to its own module...


I believe that it's best practice (according to some PEP's) that you keep import statements at the beginning of a module. You can add import statements to an __init__.py file, which will import those module to all modules inside the package.

So...it's certainly something you can do the way you're doing it, but it's discouraged and actually unnecessary.


While the other answers are mostly right, there is a reason why python allows this.

It is not smart to import redundant stuff which isn’t needed. So, if you want to e.g. parse XML into an element tree, but don’t want to use the slow builtin XML parser if lxml is available, you would need to check this the moment you need to invoke the parser.

And instead of memorizing the availability of lxml at the beginning, I would prefer to try importing and using lxml, except it’s not there, in which case I’d fallback to the builtin xml module.