Puzzle 6 Spam, Spam, Spam

Python Brain Teasers — by Miki Tebeka (15 / 40)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Send It to the Printer | TOC | User! Identify Yourself 👉

email.py

​ ​from​ ​email.message​ ​import​ EmailMessage

​ msg = EmailMessage()
​ msg[​'From'​] = ​'prince@palace.ng'​
​ msg[​'To'​] = ​'Scrooge McDuck <scoorge@disney.com>'​
​ msg.set_content(​'''​​\​
​ ​Dear Sir.​

​ ​I'm a Nigerian prince who came into some misfortune.​
​ ​...​
​ ​'''​)
​ ​print​(msg)

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will raise a ModuleNotFoundError exception.

images/hline.png

When Python looks for a module to import (e.g., email), it’ll go over the directories in sys.path and try to find a module matching the name. The first value in sys.path is ” (the empty string). ” stands for the current directory, and in the current directory you have the teaser file email.py. Python will load this email.py instead of the one in the standard library and will not find the message submodule in it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.