Python Forum

Full Version: NameError: name '...........' is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello I am very new to Python and need help with this script I found online. Every time I run it I get this error in PyCharm

FOLDER = EMPLOYMENT
NameError: name 'EMPLOYMENT' is not defined

The script is supposed to get all the email addresses in my gmail account and put them in a folder

#!/usr/bin/python

import imaplib
import sys
import email
import re

FOLDER = sys.argv[0]
FOLDER = EMPLOYMENT
LOGIN = yourusername
PASSWORD = yourpassword
IMAP_HOST = imap.gmail.com  # Change this according to your provider

email_list = []
email_unique = []

mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER) 

result, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
for i in id_list:
	typ, data = mail.fetch(i,'(RFC822)')
	for response_part in data:
		if isinstance(response_part, tuple):
			msg = email.message_from_string(response_part[1])
			sender = msg['from'].split()[-1]
			address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
	if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
		email_list.append(address)
		print(address)
On line 9, you have EMPLOYMENT. That is a variable name, and you have not defined a value for it by line 9. Variable names have to be on the left side of an assignment before they can be on the right side of an assignment.
Hi I'm new at this as this is my first python script I found it online. The program below is supposed to get all the email addresses from my inbox and save them to a folder named EMPLOYMENT. I got the program to run with no errors:

Process finished with exit code 0

but nothing happened, I didn't see one single email address. Could you please help me understand what I'm missing?

#!/usr/bin/python

import imaplib
import sys
import email
import re

FOLDER = sys.argv[0]
FOLDER = "EMPLOYMENT"
LOGIN = "[email protected]"
PASSWORD = "yourpassword"
IMAP_HOST = "imap.gmail.com"  # Change this according to your provider

email_list = []
email_unique = []

mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER) 

result, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
for i in id_list:
	typ, data = mail.fetch(i,'(RFC822)')
	for response_part in data:
		if isinstance(response_part, tuple):
			msg = email.message_from_string(response_part[1])
			sender = msg['from'].split()[-1]
			address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
	if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
		email_list.append(address)
		print(address)
As the other answer suggested, you may want to assign value to EMPLOYMENT before line 9. Or put 'EMPLOYMENT' in quotes if its the folder name
(Aug-08-2017, 03:39 AM)new2py Wrote: [ -> ]The program below is supposed to get all the email addresses from my inbox and save them to a folder named EMPLOYMENT

Actually it is looking for folder EMPLOYMENT in your mailbox and should search emails in this folder. I would comment out line#19 (the default is INBOX) and see what would happen. Note that it will just print e-mail addresses in the console. You are doing nothing with email_list that is created
Thanks, I added quotes to identify the names, the script ran successfully, but nothing happened, it did nothing.
(Aug-08-2017, 03:39 AM)new2py Wrote: [ -> ]result, data = mail.search(None, 'ALL')

That's line 21.  If you add some print functions, do you get any output?
result, data = mail.search(None, 'ALL')
print(result)
print(data)
I added the following like you said and ran in PyCharm:

#!/usr/bin/python

import imaplib
import sys
import email
import re

FOLDER = sys.argv[0]
FOLDER = "EMPLOYMENT"
LOGIN = "emailaddresshere"
PASSWORD = "passwordhere"
IMAP_HOST = "imap.gmail.com"  # Change this according to your provider

email_list = []
email_unique = []

mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER)

result, data = mail.search(None, 'ALL')
print(result)
print(data)
ids = data[0]
id_list = ids.split()
for i in id_list:
	typ, data = mail.fetch(i,'(RFC822)')
	for response_part in data:
		if isinstance(response_part, tuple):
			msg = email.message_from_string(response_part[1])
			sender = msg['from'].split()[-1]
			address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
	if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
		email_list.append(address)
		print(address)
and nothing happened all i got was this:


C:\Users\mattis\AppData\Local\Programs\Python\Python36\python.exe C:/Users/mattis/Documents/email_addy_extrac.py
OK
[b'']

Process finished with exit code 0
I found this script online that is supposed to get all the email addresses in my Inbox and print them to a folder, it successfully ran in PyCharm but nothing happened. I didn't see any email addresses, please help Wall

#!/usr/bin/python

import imaplib
import sys
import email
import re

FOLDER = sys.argv[0]
FOLDER = "EMPLOYMENT"
LOGIN = "emailaddresshere"
PASSWORD = "passwordhere"
IMAP_HOST = "imap.gmail.com"  # Change this according to your provider

email_list = []
email_unique = []

mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER)

result, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
for i in id_list:
	typ, data = mail.fetch(i,'(RFC822)')
	for response_part in data:
		if isinstance(response_part, tuple):
			msg = email.message_from_string(response_part[1])
			sender = msg['from'].split()[-1]
			address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
	if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
		email_list.append(address)
		print(address)
result after running the script:

Process finished with exit code 0
(Aug-09-2017, 05:43 PM)new2py Wrote: [ -> ]OK
[b'']
So you're communicating fine with the imap server, but it's telling you that there's no messages in the EMPLOYMENT folder.
Maybe try mail.list() (https://docs.python.org/3.6/library/imap...IMAP4.list) so you can see what it is, exactly, that the server recognizes as folder names?
Pages: 1 2