0

I am using Python 3.7 (x64) on a Windows 7 machine. I'm recieving the error message below, but when I did research on similar issues, none of the solutions appeared to relevant. The script is named master_pdf_email.py, and is inside a folder with no other .py files. This is on a newly reset computer and is the only other Py script installed, so there is no capacity for any email.py scripts to come into conflict with my current Python runtime.

  • I checked to make sure that this is running on a new, cleanly installed Python distribution.
  • I installed exchangelib 2.0.1 from pip on the same Python instance I'm using to run this.

Code up to when the error is thrown:

import pdfkit, mysql.connector, pprint, json, csv, datetime
from exchangelib import Credentials, Account, FileAttachment
from exchangelib.folders import Message, Mailbox

The traceback for the error message:

    Traceback (most recent call last):
      File "master_email_pdf.py", line 3, in <module>
        from exchangelib.folders import Message, Mailbox
    ImportError: cannot import name 'Message' from 'exchangelib.folders'
    (C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\exchangelib\folders\__init__.py)
Ethan Hill
  • 478
  • 2
  • 10
  • 24
  • 1
    From their [docs](https://pypi.org/project/exchangelib/) it appears you import `Message` straight from `exchangelib` – MyNameIsCaleb Sep 23 '19 at 18:12
  • Fascinating. This code worked without any issues on one of the development machines. I want to do some research now and figure out how that could have happened. – Ethan Hill Sep 23 '19 at 18:34
  • 1
    Namespace can be manipulated depending on how you import. – MyNameIsCaleb Sep 23 '19 at 18:35
  • If the solution answers your question please checkmark it and close this question out. – MyNameIsCaleb Sep 23 '19 at 19:03
  • 1
    On pre-2.0 versions it just happened to work because `Message` was being imported into `exchangelib.folders` as a dependency. That changed in 2.0. It's actually located in `exchangelib.items`, but imported directly into `exchangelib` as a shortcut. – Erik Cederstrand Sep 24 '19 at 06:38

2 Answers2

1

You need to import directly from exchangelib for Message and Mailbox.

There is an example showing this on their main page.

If you look at the __init__.py for the primary exchangelib directory, you'll find that Message and Mailbox are being added to __all__ which is where imports are loaded to the namespace. [code]

If you look at the folders directory below that, you won't find any Message or Mailbox and within the __init__.py you won't find it either. You could get MailboxAssociations or Messages from .folders if that is what you actually need.

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
1

I had the same problem. The problem is that you have a script called email.py which overrides the built-in email module which then fails to import.

I created a new folder and moved my project to it , then my problem solved.

Milad Saeedi
  • 389
  • 3
  • 8