Vinay
Vinay

Reputation: 1651

Weird Python import errors

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'

Upvotes: 4

Views: 143

Answers (1)

Mike Scotty
Mike Scotty

Reputation: 10782

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.

Upvotes: 1

Related Questions