2

I always use implicit imports like from module import z, y, z but now I got a project, where all imports are done like from module import * and I'm trying to understand, if there any advantages of importing everything from the other module.

Intuitively it looks for me like a very bad and inconsistent pattern, but maybe I'm wrong? Why would somewone use from module import *?

  • I dont know how python handles imports - ther emight be a caveat beacuse its an interpreted language. In compiled languages, this is 100% a matter of taste because the compiled code is the exact same with boths ways. In Java, it is considered best practice to use the * as soon as you import more than 2 or 3 items from the package. The explicit style is only used to avoid collisions. – marstato Mar 28 '17 at 8:13
  • 1
    Re "Why would someone use from module import *?" The generic advice is that you shouldn't do that. It is in fact considered rather bad form by many. For one thing, it pollutes your namespace. Even worse, it might break the imported module. – David Hammen Mar 28 '17 at 8:16
  • This is a "wildcard import", which may help you to find the dozens of questions about it on Stack Overflow – jonrsharpe Mar 28 '17 at 12:19
  • @jonrsharpe didn't know that this approach is called "wildcard import", thanks! – Paul Mar 28 '17 at 16:07
10

When you do from my_module import * it means that all functions and class declarations from my_module are available. That is the only advantage to doing this.

There are some caveats to consider:

You must know what is in the other module to know what is imported

If you don't know what functions or classes are in your 'my_module', you have to go read this module, while not intrinsically bad. New members on the project might be confused and if you go back to your code 6 months later, you might not be fully aware of what is actually imported.

Clarity is key

from my_reader import * is less clear then from my_reader import file_reader

You risk creating collisions in imports

If you declare a function or class with the same name in two different modules and import both of them into the same file, this will cause issues. Named importing allows you override the name at import

For example:

from file_reader import read as read_file
from web_reader import read as read_website

This is a much safer method of

Knowing your dependencies

When you import just the functions and classes you need, you know exactly what the module you are working on depends on. And with tools you can build these dependency graphs if you need.

When modules change the worst is ImportError

The danger of not knowing what is collected in that star, is when a function or class is moved from a module into another. The compiler will not help you when you have a star. You would get an undefined error, which is miles more confusing than ImportError, which is easier to track down.

Advice

If you want access to any part of the module it is probably best to use import my_module over from my_module import *. You can access any function or object of my_module by using the following syntax:

my_module.my_awesome_function()
my_object = my_module.MyObject()

The only disadvantage is it is slightly longer but will provide better errors. It's still better to decide this style for your own project. But consider applying a bit of clean code and offering good debugging strategies in case of developer mistakes.

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.