1

Why is it considered good style to do all imports in a python application at the beginning, even if what is imported is used only once?

I have been programming only shorter pieces of software for a few month now, around a 1-2 thousand lines of code, and it would seem to me to lead to clearer code, if imports that are used only once to be only imported at the place in the code right what is imported is used. Though all the code I saw so far has no abided to that rule.

Otherwise one sees a long list of imports at the beginning, many of which need to be used only much later - and it is unclear when; maybe the relevant pieces of code have already been replaced by better code, but the import is still lurking there, though it is not really used.
(Maybe some fancy IDE allows one to detect such problems, but I think clear code should be clear be itself not be made clear by the help of an IDE.)

The same question also for function definition - if they're not used really often, why declare them at the beginning of the current file and not at the place they are used?

7

Here are the benefits of the imports at the top of the file:

  1. As soon as I open the file, I can see all its dependencies. I know it's using datetime or os, because I can immediately see the imports appearing at the top. If imports are dispersed, the only way to find that is to grep through the file.

  2. If you do the import in a function, you may end up in a situation where you are trying to use the module before importing it.

  3. Importing a module can take time. Putting the imports at the top means it will happen as soon as you start the application. If you do the import within a function, you'll encounter the slowness during the execution of this function.

This being said:

  1. When the imports are at the top of the file, it happens that the code using the import is removed, but the import itself remains. This means that the presence of an import at the top doesn't mean the module is actually used, so greping through the file is still required.

  2. The module being used before being imported problem applies as well to everything else in Python, such as the variables, leading to a runtime error.

  3. Importing a module deep inside a conditional logic means that we'll be spending the time to import the module only when we need it, also meaning that the application will start faster.

Therefore, it all comes to the consistency:

  1. Most imports are not used only once, or they are used once at the moment you add them, and then are used in other locations. In this case, having all the imports at the top is more intuitive than having some of them at the top and some of them scattered over the file.

  2. Most projects are putting imports at the top. As with most style rules, what matters is not if it's better to do A or B, but that we all try to use the same alternative consistently. Therefore, in order to make the code more readable for other Python developers, use the convention already in use in most projects, just because it's the most popular one.

  • +1 Great explanations of the different consequences of having the imports at the top vs. deep in the code – l7ll7 Oct 20 '18 at 13:07
0

It helps understanding the code because you see all dependencies at first sight.

Long lists of imports and long files are a sign of bad design with too many dependencies. Such files should be refactored anyway.

Function definitions ore declarations are a completely different topic because they are internal and not external. I don't agree that they should be at the beginning of a file. However some languages require it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.