6

I am trying to use AWS Simple Email Service with Python. I followed the Send an Email Using the AWS SDK for Python (Boto) sample code. The program cannot correctly import boto3.

When it tries to import boto3 for the first time, Python gives error ImportError: cannot import name ClientError.

If I try to import again, the error becomes ImportError: cannot import name certs.

I have also checked the installed boto3 version and it has same version as the sample code.

boto3 Version: 1.4.4
botocore Version: 1.5.95
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
jiashenC
  • 1,812
  • 2
  • 16
  • 31

3 Answers3

14

Your versions of boto3 and botocore are very dated. Install updated versions first. I would also update your installed version of the AWS CLI.

pip install boto3 --upgrade
pip install awscli --upgrade

[UPDATE after comment]

After the updates, double check that you have at least the following versions when executing "aws --version":

aws-cli/1.14.2 Python/2.7.9 Windows/8 botocore/1.8.6

Next try to send an email from the CLI. The following is a script for the Windows CMD Prompt. Modify with valid email addresses that are verified with SES. The same command modified a bit will work from Linux.

set FROM=from@mydomain.com
set TO=to@mydomain.com
aws ses send-email --from %FROM% --destination ToAddresses=%TO% --message Subject={Data="Hello world
"},Body={Text={Data="Hello World"}}
John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • 1
    I have tried to upgrade those to latest version, but I still get the same error. – jiashenC Dec 22 '17 at 23:26
  • I have modified my answer with more steps to help debug your issue. – John Hanley Dec 23 '17 at 00:14
  • I have verified that the sample code works correctly with the current boto3 version (1.4.8) and both Python 2.7.13 and Python 3.6.1. If you continue to have problems, then your installation of boto3 is invalid. I would delete it and reinstall fresh. Also make sure that the rest of your Python system and libraries are current. – John Hanley Dec 23 '17 at 00:24
  • Thanks! AWS CLI works perfect, but Python still gets error. I have also checked libraries you mentioned. They are all updated version. I guess maybe because my boto3 installation is invalid. How do you check if it is good installation or not? I just use pip install boto3. – jiashenC Dec 23 '17 at 02:24
  • What version of Python? Just remove boto3 and reinstall. There is lots of good documentation. Fire up Google and do some homework. – John Hanley Dec 23 '17 at 21:57
  • Python 2.7.14. I am using MacOS and it works perfect under `/Users/myusername/` directory. However, if I try to run under `/Users/myusername/Desktop/`, it gives error. I try to install boto3 globally by `sudo pip install boto3` and also install boto3 user wise by `pip install boto3 --user`. Both has same issue. – jiashenC Dec 24 '17 at 15:17
  • I am not a Mac user. From your comment it looks like you have a Python library path problem. https://stackoverflow.com/questions/2741496/location-of-global-libraries-for-python-on-mac – John Hanley Dec 24 '17 at 19:23
5

Finally, it turns out that this issue is related to name shadowing. Botocore actually has a module called email and I name my file also as email.py. As a result, botocore cannot import email module correctly.

jiashenC
  • 1,812
  • 2
  • 16
  • 31
1

On Ubuntu 20.04. I had the same problem, and most suggestions didn't work.

Eventually solved by running this (not, not as sudo, installing as --user since apt is incompatible with pip)

#
# Upgrade all 3 packages together, using a more explicit version of python
#
python3.7 -m pip install --upgrade boto3 botocore awscli
arielf
  • 5,802
  • 1
  • 36
  • 48