You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中两种带别名的import导入方式是否完全一致?

Are import module.submodule as alias and from module import submodule as alias identical?

Great question—your observation is spot-on for the tkinter.ttk case you tested, but let’s break down whether these two approaches are always the same, or just coincidentally similar here.

Short Answer for Your Test Case

In the specific scenario you tried with tkinter.ttk, yes—these two import methods behave exactly the same in practice. Both end up binding the tkinter.ttk module object to the name my, so calling my.Button() will produce identical results every time.

Under-the-Hood Differences (That Rarely Matter... Until They Do)

While the end result is the same for standard library submodules like ttk, there are subtle differences in how Python resolves the imports that can cause behavior changes in edge cases:

1. How Python looks up the target

  • import tkinter.ttk as my: Python first loads the tkinter package, then navigates to its ttk submodule (loading it if needed), and finally binds that submodule to my. This path always targets the submodule directly, regardless of what’s in tkinter’s __init__.py.
  • from tkinter import ttk as my: Python looks for an object named ttk within the tkinter package’s namespace. Usually, this is the submodule, but if tkinter/__init__.py had a variable or function named ttk (e.g., ttk = "some custom value"), this import would grab that object instead of the submodule.

2. Module caching side effects

Both imports will load tkinter and tkinter.ttk into Python’s module cache (sys.modules) if they aren’t already there. The only minor difference is that import tkinter.ttk as my doesn’t add tkinter to your current namespace (you’d have to import tkinter separately if you wanted to use it), while from tkinter import ttk as my also doesn’t add tkinter to your namespace—so this is rarely a practical distinction.

Example of When They Do Differ

Let’s imagine a custom package where the __init__.py overrides a submodule name:

mypackage/
    __init__.py
    utils.py

In mypackage/__init__.py:

# This overrides the "utils" submodule name in the package namespace
utils = "I'm a string, not a module!"

In mypackage/utils.py:

def greet():
    print("Hello from the utils module!")

Now try the two imports:

  • import mypackage.utils as u: u will be the utils.py module—you can call u.greet() successfully.
  • from mypackage import utils as u: u will be the string "I'm a string, not a module!"—calling u.greet() will throw an AttributeError.

Final Takeaway

For most everyday use cases (especially with standard library modules like tkinter.ttk), these two import styles are interchangeable and produce identical results. The only time you’ll see a difference is when a package’s __init__.py defines a name that conflicts with one of its submodules.

内容的提问来源于stack exchange,提问作者RisingUnderDog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:33:38