Python Forum
NameError: name '...........' is not defined
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name '...........' is not defined
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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)
Reply
#4
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
Reply
#5
(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
Reply
#6
Thanks, I added quotes to identify the names, the script ran successfully, but nothing happened, it did nothing.
Reply
#7
(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)
Reply
#8
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
Reply
#9
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
Reply
#10
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 233 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,084 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,860 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,279 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,494 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,907 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,315 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,891 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,347 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,867 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020