Which Python import style is better: from foo import bar or import foo.bar as bar?

Should I Use from foo import bar or import foo.bar as bar When Importing a Module Without Changing the Name?

When importing a module, if there is no need or desire to change the name (i.e., bar), should I use:

from foo import bar

OR

import foo.bar as bar

Are there any differences between these two approaches? Does it matter which one I use in Python?

Python import as: Are there any situations where one is more preferred over the other, or is it purely a matter of style?

Hey All!

Direct Import (from foo import bar): Using from foo import bar is the simplest approach when you want to import a specific function, class, or module (bar) from another module (foo). It directly brings the imported object into the current namespace, making it clean and easy to use without needing the module prefix (foo.) every time.

This is ideal when you only need one or a few specific elements from a module and want to keep your namespace clean. However, it can sometimes lead to naming conflicts if multiple modules have the same function or class names.

Example:

from foo import bar  
bar.some_function()  # No need to prefix with foo.bar  

This style is efficient for targeted imports, ensuring simplicity and readability in smaller scripts.

Hello Everyone,

Building on what @panchal_archanaa said, there’s another approach:

Import with Alias (import foo.bar as bar). This method is particularly useful when you need the full module (foo.bar) but want to use a shorter alias (bar). It’s especially helpful when dealing with long module names or when avoiding naming conflicts across your codebase.

Unlike from foo import bar, this approach retains the module’s original namespace while still offering the flexibility of a concise alias. Additionally, it works well in scenarios where you need to access multiple elements from the module without importing them individually.

Example:

import foo.bar as bar  
bar.some_function()  # Access through the alias 'bar'  

This style, leveraging python import as, is a great choice for larger, more complex projects where readability and modularity are key.

@panchal_archanaa and @netra.agarwal have covered the mechanics well, so let’s look at when to use one style over the other:

  • Use from foo import bar for cleaner namespaces when you only need a few specific elements from the module. It keeps your code lightweight and straightforward.
  • Use import foo.bar as bar when the module name is long, you need access to multiple elements, or you want to avoid naming conflicts. The aliasing feature (python import as) simplifies module usage without losing clarity.

For example, if you’re working with a module like some_very_long_module_name.utils, using:

import some_very_long_module_name.utils as utils  
utils.some_function()  

is far more convenient than typing the full module name repeatedly.

Ultimately, the choice depends on the scope of your project and the balance between simplicity and modularity.