4

I'm having this really weird error where I'm getting import errors in a Python shell launched from a home directory of an AWS EMR whereas the same packages are getting imported fine when I cd into another directory and launch the Python shell there.

The EMR has Python 2.7 installed. Please let me know what additional information I can provide. This issue is driving me crazy!

$ python
Python 2.7.16 (default, Mar 18 2019, 18:38:44)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import smtplib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/smtplib.py", line 46, in <module>
    import email.utils
  File "email.py", line 2, in <module>
    from email.mime.multipart import MIMEMultipart
ImportError: No module named mime.multipart
>>> os.path.realpath('./')
'/home/abc123'
>>>
$ mkdir temp
$ cd temp
[temp]$ python
Python 2.7.16 (default, Mar 18 2019, 18:38:44)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import smtplib
>>> os.path.realpath('./')
'/home/abc12/temp'

2
  • 3
    Is there a file named email.py in the first (home) directory? Or a folder named email? If yes, then this can influence the import behaviour, due to python's lookup mechanics (app root takes precedence over other imports). Commented Jul 15, 2019 at 8:23
  • Unbelievable! Dumb guy me created a test script to test the email functionality and named it email.py. Thank you! Do you want to change your comment into an answer so that I can accept it? Commented Jul 15, 2019 at 8:32

1 Answer 1

1

Is there a file named email.py in the first (home) directory? Or a folder named email? If yes, then this can influence the import behaviour, due to python's lookup mechanics (app root takes precedence over other imports).

From the docs: (emphasis mine)

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.