import adds things to the namespace. When Python encounters a word that isn't a Python keyword (for, while, if, else...) it looks for the word in the namespace. If the word is not in the namespace Python raises a NameError.
root = tkinter.Tk()
Output:
Traceback (most recent call last):
File "C:\python_musings\junk.py", line 1, in <module>
root = tkinter.Tk()
NameError: name 'tkinter' is not defined
But if I import tkinter Python can find 'tkinter' in the namespace and find "things" that are defined by the tkinter module.
import tkinter
print(tkinter)
root = tkinter.Tk() # uses tkinter module to find tkinter.Tk()
Output:
<module 'tkinter' from 'C:\\Program Files\\Python38\\lib\\tkinter\\__init__.py'>
If you don't import any modules your program is limited to using Python keywords and a few classes. You can use dir() to get a list of everything in the current namespace:
for name in dir():
print(name)
Output:
__annotations__
__builtins__
__doc__
__file__
__loader__
__name__
__package__
__spec__
Doesn't look like much, but if you start looking at the classes you'll find that each adds a lot of abilities to Python.
for name in dir(__builtins__):
print(name)
Output:
ArithmeticError
AssertionError
AttributeError
BaseException
BlockingIOError
BrokenPipeError
BufferError
BytesWarning
ChildProcessError
ConnectionAbortedError
ConnectionError
ConnectionRefusedError
ConnectionResetError
DeprecationWarning
EOFError
Ellipsis
EnvironmentError
Exception
False
...
from tkinter import Tk
adds Tk to the namespace.
from tkinter import Tk
for name in dir():
print(name)
Output:
Tk
__annotations__
__builtins__
__doc__
__file__
__loader__
__name__
__package__
__spec__
Now I can use Tk in my program because 'Tk' is defined in the namespace. Notice however that tkinter is not defined in the namespace and using tkinter.Tk() raises a NameError exception.
from tkinter import Tk
root1 = Tk()
root2 = tkinter.Tk()
Output:
Traceback (most recent call last):
File "C:\python_musings\junk.py", line 3, in <module>
root2 = tkinter.Tk()
NameError: name 'tkinter' is not defined
In addition to adding things to the namespace, import executes the code in the imported module. I have two files; junk.py and junk2.py
junk2.py
def printit(a):
print('printing ', a)
printit('Hello world')
junk.py
import junk2
junk2.printit('Hi Mom!')
When I execute the command python junk.py this is the output:
Output:
printing Hello world
printing Hi Mom!
Not only does this program execute the printit() call inside junk.py, it also executes the printit() call inside junk2 when junk2 is imported.
Sometimes you want to execute code in an imported module, but sometimes you only want to execute code if the module is the "main" file, the file provide to the python command. This is why at the bottom of many Python modules (files) you will see this:
if __name__ == '__main__':
# conditionally executed code goes here
This if statement only executes when the file is being run by Python as the main file, not when the file is imported.