I have a simple test script:
import requests
response = requests.get('http://httpbin.org/get')
print response.text
It works when the python script is named test.py
but fails if named email.py
or logging.py
:
Traceback (most recent call last):
File "./email.py", line 3, in <module>
import requests
File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 53, in <module>
from urllib3.contrib import pyopenssl
File "/usr/lib/python2.7/dist-packages/urllib3/__init__.py", line 16, in <module>
from .connectionpool import (
File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 59, in <module>
from .request import RequestMethods
File "/usr/lib/python2.7/dist-packages/urllib3/request.py", line 12, in <module>
from .filepost import encode_multipart_formdata
File "/usr/lib/python2.7/dist-packages/urllib3/filepost.py", line 15, in <module>
from .fields import RequestField
File "/usr/lib/python2.7/dist-packages/urllib3/fields.py", line 7, in <module>
import email.utils
File "/home/ubuntu/temp/email.py", line 4, in <module>
response = requests.get('http://httpbin.org/get')
AttributeError: 'module' object has no attribute 'get'
It appears that requests
imports urllib3
, which imports email
built-in module. Why is Python not finding the built-in email
module first, instead of looking in the current path for email.py
?
Is there a way to make this work, or do I just have to always avoid naming my Python scripts any built-in module that may be imported by any dependency?