I am working on a simple IMAP client written in Python. Once I got an initial script working (in that I could download and show the user a list of mailboxes), I decided it was time to get the code more organized.
I broke up the IMAP related functions into a module name imap.py and the user-interaction + CUI related functions into a module name mail.py. I imported imap.py into mail.py, but when I try to call the imap_functions class I get a "name not defined" error. I'm confused, because I define the class imap_functions at the beginning of imap.py, which is, as I said, imported into the main script.
Below is the code for the two scripts:
# mail.py
import py_cui
import imap
#from itertools import chain
host = "imap.gmail.com"
username = "user@gmail.com"
password = "someappapassword"
imap_functions = imap_functions(host, username, password)
class Mail:
def __init__(self, master):
self.master = master
# Creating widgets for mailbox list on left and message list onright
# Make Mailbox widget and get a list of folders for account
Mailboxes = self.master.add_scroll_menu('Mailboxes', 0, 0, row_span=6, column_span=1)
Mailboxes.add_item_list(imap_functions.get_folders())
# Make a Messages widget and get a list of messages based on the folder selected.
Messages = self.master.add_scroll_menu('Messages', 0, 1, row_span=6, column_span=4)
# First select the folder
# message_list = client....whatever method lists messages
# Messages.add_item_list(message_list)
# Create the CUI with 7 rows 6 columns (may change later), pass it to the wrapper object, and start it
root = py_cui.PyCUI(7, 6)
root.set_title('Mail')
s = Mail(root)
imap_functions.login()
root.start()
and
### imap.py
import ssl
import imapclient
import email
class imap_functions:
def __init__(self, host, username, password):
self.host = host
self.username = username
self.password = password
self.client = IMAPClient(host)
def login(self, username, password):
"Handling various auth protocols"
#client.starttls() --- gmail does not support starttls
client.login(username, password)
return;
def get_folders(self):
"Grabbing the list of folders for the account"
get_folders = client.list_folders()
folder_list = []
for folders in get_folders:
folder_list.append(folders[2])
return folder_list;
#client.select_folder("INBOX")
You could either change the import to be
from imap import imap_functions
# ...
imap_functions = imap_functions(host, username, password)
Or as pointed out by @mkrieger1 you can access the class from the module by calling
import imap
# ...
imap_functions = imap.imap_functions(host, username, password)
I would also recommend changing the class name to ImapFunctions
since snake_case is usually used for variables and functions. Ideally, you could change the name to something even nicer, like ImapClient
but I guess that's a matter of choice and who will see your code besides you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.