Python Forum
TypeError: initial_value must be str or None, not bytes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: TypeError: initial_value must be str or None, not bytes (/thread-4624.html)



TypeError: initial_value must be str or None, not bytes - new2py - Aug-30-2017

Hello, I am new to Python, got my first script (that I found online) working. The problem is my PyCharm trail period has expired, so I just installed LiClipse and now my script doesn't work. The script extracts email addresses from your Inbox that's inside a folder:

#!/usr/bin/python

import imaplib
import sys
import email
import re

FOLDER = sys.argv[0]
FOLDER = "EMPLOYMENT3"
LOGIN = "emailaddresshere"
PASSWORD = "passwordhere"
IMAP_HOST = "imap.mail.yahoo.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)
This is the error message I get ever since I switched from PyCharm to LiClipse:



TypeError: initial_value must be str or None, not bytes


RE: TypeError: initial_value must be str or None, not bytes - snippsat - Aug-30-2017

Quote:The problem is my PyCharm trail period has expired
Use the free community edition of PyCharm.
Quote:so I just installed LiClipse and now my script doesn't work.
The error message show now that you use Python 3(as you should),
and have decode content to be a string.
The error message will also show line number,that you have not posted.
I guess change line 22 to this will work.
ids = data[0].decode()



RE: TypeError: initial_value must be str or None, not bytes - new2py - Aug-30-2017

I tried installing line 22, that didn't work, same result
I installed the community version of PyCharm (thanks for the tip) that didn't work, same result

it was working perfectly with the professional version, code is exactly the same


RE: TypeError: initial_value must be str or None, not bytes - snippsat - Aug-30-2017

(Aug-30-2017, 11:03 PM)new2py Wrote: it was working perfectly with the professional version, code is exactly the same
Did you use Python 2 or 3? 
(Aug-30-2017, 11:03 PM)new2py Wrote: I installed the community version of PyCharm (thanks for the tip) that didn't work, same result
Try change line 26 to:
for response_part in data.decode():



RE: TypeError: initial_value must be str or None, not bytes - new2py - Aug-31-2017

Thanks snippsat,

I found the fix right before i saw your reply
and like you said, change the interpreter from Python 3.6 to 2.7
Now the script is running again!!!
I keep running into this issue,
what are some common compatibility issues between 2.7 and 3.6?