0

I've been trying to import pandas into my python file but every time i run it i get the following error:

 Traceback (most recent call last):


File "C:\Python36\lib\site-packages\pandas\compat\__init__.py", line 49, in <module>
    import __builtin__ as builtins
ModuleNotFoundError: No module named '__builtin__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "email.py", line 1, in <module>
    import pandas as pd
  File "C:\Python36\lib\site-packages\pandas\__init__.py", line 23, in <module>
    from pandas.compat.numpy import *
  File "C:\Python36\lib\site-packages\pandas\compat\__init__.py", line 62, in <module>
    import http.client as httplib
  File "C:\Python36\lib\http\client.py", line 71, in <module>
    import email.parser
  File "C:\Users\xx\Desktop\Projects\email_sender\email.py", line 3, in <module>
    df = pd.read_excel("emails.xlsx", 'Sheet1', index_col=None, na_values=['NA'])
AttributeError: module 'pandas' has no attribute 'read_excel'

this is what the import code block looks like in the the /pandas/__init__.py file:

try:
    import __builtin__ as builtins
    # not writeable when instantiated with string, doesn't handle unicode well
    from cStringIO import StringIO as cStringIO
    # always writeable
    from StringIO import StringIO
    BytesIO = StringIO
    import cPickle
    import httplib
except ImportError:
    import builtins
    from io import StringIO, BytesIO
    cStringIO = StringIO
    import pickle as cPickle
    import http.client as httplib

I found out that __builtin__ was renamed to builtins in python3, after fixing that line and trying to import I get the error from the StringIO import as well, since that was deleted from python3 and uses a different syntax to call that module.

My system only has python3 installed. I have upgraded all pandas dependencies as well and they can be imported as normal. I've also uninstalled pandas and reinstalled again but the same thing happens.

Pandas version is 0.23.1 Python version is 3.6.5

I don't know why the latest version of pandas is using deprecated or deleted modules in its imports.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Bridgie
  • 1
  • 4
  • You've named your script `pandas.py`. Change it, like, right now. – cs95 Jun 22 '18 at 18:05
  • The code is designed to run on both Python 2 and Python 3. Note the `try:...except ImportError:` part, in Python 3 the first `import __builtins__` fails, the exception is caught , and the other block is being executed. – Martijn Pieters Jun 22 '18 at 18:07
  • 1
    However, **something else is going wrong**, in that you masked the name `email`. Don't name your script `email.py`, it is being imported when the `http.client` module tries to import `email.parser`. This is evident from the `During handling of the above exception, another exception occurred` section. – Martijn Pieters Jun 22 '18 at 18:08
  • @coldspeed: no, they named it `email.py`. – Martijn Pieters Jun 22 '18 at 18:08
  • @MartijnPieters it would be funny if email.py was calling a user-defined pandas.py which was trying to import pandas. The question's pretty unclear either way. – cs95 Jun 22 '18 at 18:11
  • 1
    What happens is: You used `import pandas as pd`. This triggers various imports, including the `import __builtins__` line. That raises an `ImportError` because you are on Python 3, and this is *expected behaviour and normal*. The exception is handled. But as the exception is being handled, the line `import http.client as httplib` is executed, which executes `import email.parser` which triggers your `email.py` being loaded again, at which point `pandas` is incomplete and `pd.read_excel` isn't set yet. – Martijn Pieters Jun 22 '18 at 18:12
  • Because `pd.read_excel` doesn't exist yet, the code there raises an `AttributeError` exception and because that was raised during the `ImportError` handling in `pandas.compat`, you see the extra information about that import error first. And that's a red herring, because *that part is not the error*, that exception was already being handled. – Martijn Pieters Jun 22 '18 at 18:13
  • renaming the email.py to email_runner.py fixed the issue. I never considered that naming the python file that, would break pandas so easily. Thanks for the answers – Bridgie Jun 22 '18 at 18:18

0 Answers0