Python: `from x import *` not importing everything

前端 未结 3 842
独厮守ぢ
独厮守ぢ 2020-12-16 20:55

I know that import * is bad, but I sometimes use it for quick prototyping when I feel too lazy to type or remember the imports

I am trying the following

3条回答
  •  难免孤独
    2020-12-16 21:31

    I learned this from my own situation. A module did not automatically import along with the rest of the package. Before that experience my mistaken understanding was that every packages's modules automatically import from an import x or a from x import *. They don't.

    Beginners might expect EVERYTHING to import under those calls, I believe. But the following GUI programming code, which is common, demonstrates that that's not the case:

    from tkinter import * 
    from tkinter import ttk
    

    In the above example, module ttk doesn't import automatically along with the rest of the tkinter package, for instance.

    The explanation that I've been told is as follows: when you use from x import *, you actually only imported things in your-python-location/lib/x/__init__.py

    Packages are folders. Modules are files. If the import calls for specific files then the package folder's __init_.py will enumerate the specific files to import.

提交回复
热议问题