Import all and import certain elements in Python - python

I am currently learning Python 3.xx (3,8 currently to be more specific) as first language and am seeing all the time something like
from tkinter import *
from tkinter import font
Now my question here is...:
when you import from tkiner *(therefore all) why do you import again certain elements? Should it not be included in all ( * )?
Thank you in advance for your time and effort spent on answering my question and have a wonderfull day.

The second line will import tkinter.font, which is a submodule.
import * won't import submodules.
>>> from tkinter import *
>>> font
>>> from tkinter import font
>>> font
<module 'tkinter.font' from 'lib/python3.7/tkinter/font.py'>
Another case where * would not import everything is when the module has an __all__ attribute. (Search for "public names" in the documentation here.)

Related

Import tkinter/Tkinter as "Tk" or "tk"?

I've seen the light an will now convert all my tkinter references from Canvas to Tk.Canvas, from Label to tk.Label, and from root = Tk() to root = tk.Tk(), etc.
Stuck in between Python 2.7.12 and Python 3.5 my program stub looks like this:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
import tkinter as Tk
import tkinter.ttk as ttk
import tkinter.font as font
except ImportError: # Python 2
import Tkinter as Tk
import ttk
import tkFont as font
The question is should I be using import tkinter as Tk or import tkinter as tk?
I'm thinking the latter but I would like to stick to industry standards. Also take into consideration how most answers are written in Stack Overflow. Going with this majority means I can try code snippets with the least modifications.
The advantage I see using import tkinter as tk is:
Tkk is imported as ttk so there is capitalization consistency.
tkFont is imported as font so there is capitalization consistency.
I'm still on my first Python project and want to develop good reusable code for future projects.
The question is should I be using import tkinter as Tk or import tkinter as tk?
You should use import tkinter as tk, based on the naming conventions in pep8

Why import duplicates from a module [duplicate]

This question already has an answer here:
Why use Python code "from tkinter import ttk"? [duplicate]
(1 answer)
Closed 4 years ago.
I am learning tkinter and noticed that people import multiple things sometimes.
from tkinter import *
from tkinter import ttk
I was wondering why people do this for many modules, not just tkinter. I always thought that import * meant that you were importing everything from a module. So why do people import more items?
tkinter.ttk is a submodule of tkinter. Submodules aren't guaranteed to be loaded by import *; if you want them, it's safest to import them yourself. (Whether submodules are loaded by import * depends on the presence and contents of an __all__ list, as well as what imports __init__.py performs and what imports have already been performed.)

Why use Python code "from tkinter import ttk"? [duplicate]

This question already has answers here:
Python: `from x import *` not importing everything
(3 answers)
Closed 6 years ago.
Python applications using TK GUIs usually call the following import statements:
from tkinter import *
from tkinter import ttk
Isn't calling from tkinter import ttk redundant? I thought ttk would already be imported by calling from tkinter import *
Please explain why ttk needs to be imported separately?
(I already understand that it has "improved widgets". I want to understand why the improved widgets aren't accessible from the from tkinter import * call.)
If you go to your python installed location, you will find that in the python library, tkinter is a folder instead of a .py file. So when you use from tkinter import *, you actually only imported things in your-python-location/lib/tkinter/__init__.py. Things like ttk are actually separate files in tkinter folder(e.g. lib/tkinter/ttk.py, lib/tkinter/scrolledtext.py etc.) Therefore, from tkinter import * and from tkinter import tkk are different commands that import things from different module.

Importing a module and using its methods

I am looking at a snippet of code and I just don't understand how it works:
import pygame, sys
from pygame.locals import *
on the first line pygame is imported, and on the second line, all the methods of a subset of pygame is invoked. If the first line imports all of pygame, why do we have to specifically import a subset of the module again? Why doesn't a mere import pygame do the job in the first place?
A mere import pygame would suffice, but the author wanted to have a shorthand access to the constants of pygame. For example, instead of:
import pygame
...
resolution = pygame.locals.TIMER_RESOLUTION
it may be sometimes preferable to have
import pygame
from pygame.locals import *
...
resolution = TIMER_RESOLUTION
Note that you should still import pygame itself to be able to access to other methods/properties (other than pygame.locals.) of pygame.
The idea is that you can call all the functions in pygame.locals without using pygame.locals.someFunction, but instead someFunction.

Difference between import tkinter as tk and from tkinter import

I know it is a stupid question but I am just starting to learn python and i don't have good knowledge of python. My question is what is the difference between
from Tkinter import *
and
import Tkinter as tk
?Why can't i just write
import Tkinter
Could anyone spare a few mins to enlighten me?
from Tkinter import * imports every exposed object in Tkinter into your current namespace.
import Tkinter imports the "namespace" Tkinter in your namespace and
import Tkinter as tk does the same, but "renames" it locally to 'tk' to save you typing
let's say we have a module foo, containing the classes A, B, and C.
Then import foo gives you access to foo.A, foo.B, and foo.C.
When you do import foo as x you have access to those too, but under the names x.A, x.B, and x.C.
from foo import * will import A, B, and C directly in your current namespace, so you can access them with A, B, and C.
There is also from foo import A, C wich will import A and C, but not B into your current namespace.
You can also do from foo import B as Bar, which will make B available under the name Bar (in your current namespace).
So generally: when you want only one object of a module, you do from module import object or from module import object as whatiwantittocall.
When you want some modules functionality, you do import module, or import module as shortname to save you typing.
from module import * is discouraged, as you may accidentally shadow ("override") names, and may lose track which objects belong to wich module.
You can certainly use
import Tkinter
However, if you do that, you'd have to prefix every single Tk class name you use with Tkinter..
This is rather inconvenient.
On the other hand, the following:
import Tkinter as tk
sidesteps the problem by only requiring you to type tk. instead of Tkinter..
As to:
from Tkinter import *
it is generally a bad idea, for reasons discussed in Should wildcard import be avoided?
Writing:
from tkinter import *
results in importing everything that exists in tkinter module
Writing:
import tkinter
results in importing tkinter module but if you do so, in order to be able to call any method you will have to use:
tkinter.function_name()

Categories