aws cli 実行時にpython 2.7 ImportError発生

AmazonLinux_CentOS,AWS,EC2

概要

  • トラブル備忘録です。昨日AMIコピーで起動したCentOS 環境において、aws cli コマンドが下記のエラーとなり使用できない状況に遭遇…。次回別の方がこの状況に陥った時のために、対処方法を記載します。
  • aws cliコマンドのエラーは、configなどを読み込む以前であり、python のライブラリに起因した問題の様です。

# aws
Traceback (most recent call last):
  File "/bin/aws", line 19, in
    import awscli.clidriver
  File "/usr/lib/python2.7/site-packages/awscli/clidriver.py", line 17, in
    import botocore.session
  File "/usr/lib/python2.7/site-packages/botocore/session.py", line 29, in
    import botocore.configloader
  File "/usr/lib/python2.7/site-packages/botocore/configloader.py", line 19, in
    from botocore.compat import six
  File "/usr/lib/python2.7/site-packages/botocore/compat.py", line 26, in
    from urllib3 import exceptions
  File "/usr/lib/python2.7/site-packages/urllib3/__init__.py", line 10, in
    from .connectionpool import (
  File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 31, in
    from .connection import (
  File "/usr/lib/python2.7/site-packages/urllib3/connection.py", line 45, in
    from .util.ssl_ import (
  File "/usr/lib/python2.7/site-packages/urllib3/util/__init__.py", line 4, in
    from .request import make_headers
  File "/usr/lib/python2.7/site-packages/urllib3/util/request.py", line 5, in
    from ..exceptions import UnrewindableBodyError
ImportError: cannot import name UnrewindableBodyError
 

対処方法

pip のアップデート

  • お約束となりますが、pip listコマンド実行時に警告が表示されたため、念のためpipをアップデートすることに。
  • pipのversionは、19.1.1 ⇒ 19.3.1 に更新されました。

# pip list
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Package                          Version
-------------------------------- --------
awscli                           1.16.162
Babel                            0.9.6
backports.ssl-match-hostname     3.5.0.1
botocore                         1.12.152
chardet                          2.2.1
cloud-init                       18.5
colorama                         0.3.9
configobj                        4.7.2
decorator                        3.4.0
docutils                         0.14
futures                          3.2.0
iniparse                         0.4
ipaddress                        1.0.16
IPy                              0.75
Jinja2                           2.7.2
jmespath                         0.9.4
jsonpatch                        1.2
jsonpointer                      1.9
kitchen                          1.1.1
MarkupSafe                       0.11
perf                             0.1
pip                              19.1.1
policycoreutils-default-encoding 0.1
prettytable                      0.7.2
pyasn1                           0.4.5
pycurl                           7.19.0
pygobject                        3.22.0
pygpgme                          0.3
pyliblzma                        0.5.3
pyserial                         2.6
python-dateutil                  2.8.0
python-linux-procfs              0.4.9
pyudev                           0.15
pyxattr                          0.5.1
PyYAML                           3.10
requests                         2.6.0
rsa                              3.4.2
s3transfer                       0.2.0
schedutils                       0.4
seobject                         0.1
sepolicy                         1.1
setuptools                       0.9.8
six                              1.9.0
urlgrabber                       3.10
urllib3                          1.24.3
virtualenv                       16.6.0
wheel                            0.33.4
yum-metadata-parser              1.1.4
WARNING: You are using pip version 19.1.1, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
# pip install --upgrade pip
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl (1.4MB)
     |????????????????????????????????| 1.4MB 8.0MB/s
Installing collected packages: pip
  Found existing installation: pip 19.1.1
    Uninstalling pip-19.1.1:
      Successfully uninstalled pip-19.1.1
Successfully installed pip-19.3.1
# pip list
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Package                          Version
-------------------------------- --------
awscli                           1.16.162
Babel                            0.9.6
backports.ssl-match-hostname     3.5.0.1
botocore                         1.12.152
chardet                          2.2.1
cloud-init                       18.5
colorama                         0.3.9
configobj                        4.7.2
decorator                        3.4.0
docutils                         0.14
futures                          3.2.0
iniparse                         0.4
ipaddress                        1.0.16
IPy                              0.75
Jinja2                           2.7.2
jmespath                         0.9.4
jsonpatch                        1.2
jsonpointer                      1.9
kitchen                          1.1.1
MarkupSafe                       0.11
perf                             0.1
pip                              19.3.1
policycoreutils-default-encoding 0.1
prettytable                      0.7.2
pyasn1                           0.4.5
pycurl                           7.19.0
pygobject                        3.22.0
pygpgme                          0.3
pyliblzma                        0.5.3
pyserial                         2.6
python-dateutil                  2.8.0
python-linux-procfs              0.4.9
pyudev                           0.15
pyxattr                          0.5.1
PyYAML                           3.10
requests                         2.6.0
rsa                              3.4.2
s3transfer                       0.2.0
schedutils                       0.4
seobject                         0.1
sepolicy                         1.1
setuptools                       0.9.8
six                              1.9.0
urlgrabber                       3.10
urllib3                          1.24.3
virtualenv                       16.6.0
wheel                            0.33.4
yum-metadata-parser              1.1.4
# pip -V
pip 19.3.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
 

aws cli のアップデート

  • 先ず疑ったことは、aws cli が破損したと考え、aws cli のアップデートを行いました。しかし、現象は改善されませんでした。pip を使ったアップデートは、"-U" オプションを使用します。

# pip install -U awscli
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting awscli
  Downloading https://files.pythonhosted.org/packages/37/e1/ca2ee8f55258dbb976106420adecdf52db6417216199bd1392acd1fff9f7/awscli-1.16.299-py2.py3-none-any.whl (2.5MB)
     |????????????????????????????????| 2.5MB 11.2MB/s
Requirement already satisfied, skipping upgrade: docutils<0.16,>=0.10 in /usr/lib/python2.7/site-packages (from awscli) (0.14)
Requirement already satisfied, skipping upgrade: PyYAML<5.2,>=3.10; python_version != "2.6" and python_version != "3.3" in /usr/lib64/python2.7/site-packages (from awscli) (3.10)
Requirement already satisfied, skipping upgrade: rsa<=3.5.0,>=3.1.2 in /usr/lib/python2.7/site-packages (from awscli) (3.4.2)
Requirement already satisfied, skipping upgrade: colorama<0.4.2,>=0.2.5; python_version != "2.6" and python_version != "3.3" in /usr/lib/python2.7/site-packages (from awscli) (0.3.9)
Requirement already satisfied, skipping upgrade: s3transfer<0.3.0,>=0.2.0 in /usr/lib/python2.7/site-packages (from awscli) (0.2.0)
Collecting botocore==1.13.35
  Downloading https://files.pythonhosted.org/packages/1a/69/abe9276bc3699aa89cd5f743982aa9d9ce929d7f95b4900bba60922747b0/botocore-1.13.35-py2.py3-none-any.whl (5.8MB)
     |????????????????????????????????| 5.8MB 67.6MB/s
Requirement already satisfied, skipping upgrade: pyasn1>=0.1.3 in /usr/lib/python2.7/site-packages (from rsa<=3.5.0,>=3.1.2->awscli) (0.4.5)
Requirement already satisfied, skipping upgrade: futures<4.0.0,>=2.2.0; python_version == "2.6" or python_version == "2.7" in /usr/lib/python2.7/site-packages (from s3transfer<0.3.0,>=0.2.0->awscli) (3.2.0)
Requirement already satisfied, skipping upgrade: jmespath<1.0.0,>=0.7.1 in /usr/lib/python2.7/site-packages (from botocore==1.13.35->awscli) (0.9.4)
Requirement already satisfied, skipping upgrade: python-dateutil<2.8.1,>=2.1; python_version >= "2.7" in /usr/lib/python2.7/site-packages (from botocore==1.13.35->awscli) (2.8.0)
Requirement already satisfied, skipping upgrade: urllib3<1.26,>=1.20; python_version == "2.7" in /usr/lib/python2.7/site-packages (from botocore==1.13.35->awscli) (1.24.3)
Requirement already satisfied, skipping upgrade: six>=1.5 in /usr/lib/python2.7/site-packages (from python-dateutil<2.8.1,>=2.1; python_version >= "2.7"->botocore==1.13.35->awscli) (1.9.0)
Installing collected packages: botocore, awscli
  Found existing installation: botocore 1.12.152
    Uninstalling botocore-1.12.152:
      Successfully uninstalled botocore-1.12.152
  Found existing installation: awscli 1.16.162
    Uninstalling awscli-1.16.162:
      Successfully uninstalled awscli-1.16.162
Successfully installed awscli-1.16.299 botocore-1.13.35
 

aws cli の再インストール

  • 次に、aws cli をアンインストール ⇒ 再インストールしましたが、未だ現象は改善されません。

# pip uninstall awscli
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Uninstalling awscli-1.16.299:
  Would remove:
    /usr/bin/aws
    /usr/bin/aws.cmd
    /usr/bin/aws_bash_completer
    /usr/bin/aws_completer
    /usr/bin/aws_zsh_completer.sh
    /usr/lib/python2.7/site-packages/awscli-1.16.299.dist-info/*
    /usr/lib/python2.7/site-packages/awscli/*
Proceed (y/n)? y
  Successfully uninstalled awscli-1.16.299
# pip install awscli
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting awscli
  Using cached https://files.pythonhosted.org/packages/37/e1/ca2ee8f55258dbb976106420adecdf52db6417216199bd1392acd1fff9f7/awscli-1.16.299-py2.py3-none-any.whl
Requirement already satisfied: docutils<0.16,>=0.10 in /usr/lib/python2.7/site-packages (from awscli) (0.14)
Requirement already satisfied: PyYAML<5.2,>=3.10; python_version != "2.6" and python_version != "3.3" in /usr/lib64/python2.7/site-packages (from awscli) (3.10)
Requirement already satisfied: rsa<=3.5.0,>=3.1.2 in /usr/lib/python2.7/site-packages (from awscli) (3.4.2)
Requirement already satisfied: colorama<0.4.2,>=0.2.5; python_version != "2.6" and python_version != "3.3" in /usr/lib/python2.7/site-packages (from awscli) (0.3.9)
Requirement already satisfied: s3transfer<0.3.0,>=0.2.0 in /usr/lib/python2.7/site-packages (from awscli) (0.2.0)
Requirement already satisfied: botocore==1.13.35 in /usr/lib/python2.7/site-packages (from awscli) (1.13.35)
Requirement already satisfied: pyasn1>=0.1.3 in /usr/lib/python2.7/site-packages (from rsa<=3.5.0,>=3.1.2->awscli) (0.4.5)
Requirement already satisfied: futures<4.0.0,>=2.2.0; python_version == "2.6" or python_version == "2.7" in /usr/lib/python2.7/site-packages (from s3transfer<0.3.0,>=0.2.0->awscli) (3.2.0)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /usr/lib/python2.7/site-packages (from botocore==1.13.35->awscli) (0.9.4)
Requirement already satisfied: python-dateutil<2.8.1,>=2.1; python_version >= "2.7" in /usr/lib/python2.7/site-packages (from botocore==1.13.35->awscli) (2.8.0)
Requirement already satisfied: urllib3<1.26,>=1.20; python_version == "2.7" in /usr/lib/python2.7/site-packages (from botocore==1.13.35->awscli) (1.24.3)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/site-packages (from python-dateutil<2.8.1,>=2.1; python_version >= "2.7"->botocore==1.13.35->awscli) (1.9.0)
Installing collected packages: awscli
Successfully installed awscli-1.16.299
 

関連するsix パッケージを含むaws cli の再インストール

  • 次に--ignore-installed six のオプションを付けます。この方法によって解決しました。状況より、six のパッケージで提供されているライブラリが破損していたと思われます。sixはPython 2 と 3 の互換性ライブラリのようです。
  • pipの–ignore-installed オプションは、インストール済みのパッケージを再インストールするオプションです。通常、pipによるパッケージのインストールは、インストール済みの関連するパッケージの再インストールをskip しますが、このオプションで強制的に再インストールします。

# sudo pip uninstall awscli
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Uninstalling awscli-1.16.299:
  Would remove:
    /usr/bin/aws
    /usr/bin/aws.cmd
    /usr/bin/aws_bash_completer
    /usr/bin/aws_completer
    /usr/bin/aws_zsh_completer.sh
    /usr/lib/python2.7/site-packages/awscli-1.16.299.dist-info/*
    /usr/lib/python2.7/site-packages/awscli/*
Proceed (y/n)? y
  Successfully uninstalled awscli-1.16.299
# sudo -H pip install awscli --ignore-installed six
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting awscli
  Using cached https://files.pythonhosted.org/packages/37/e1/ca2ee8f55258dbb976106420adecdf52db6417216199bd1392acd1fff9f7/awscli-1.16.299-py2.py3-none-any.whl
Collecting six
  Downloading https://files.pythonhosted.org/packages/65/26/32b8464df2a97e6dd1b656ed26b2c194606c16fe163c695a992b36c11cdf/six-1.13.0-py2.py3-none-any.whl
Collecting docutils<0.16,>=0.10
  Downloading https://files.pythonhosted.org/packages/3a/dc/bf2b15d1fa15a6f7a9e77a61b74ecbbae7258558fcda8ffc9a6638a6b327/docutils-0.15.2-py2-none-any.whl (548kB)
     |????????????????????????????????| 552kB 6.4MB/s
Collecting PyYAML<5.2,>=3.10; python_version != "2.6" and python_version != "3.3"
  Downloading https://files.pythonhosted.org/packages/e3/e8/b3212641ee2718d556df0f23f78de8303f068fe29cdaa7a91018849582fe/PyYAML-5.1.2.tar.gz (265kB)
     |????????????????????????????????| 266kB 89.9MB/s
Collecting rsa<=3.5.0,>=3.1.2
  Using cached https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl
Collecting colorama<0.4.2,>=0.2.5; python_version != "2.6" and python_version != "3.3"
  Downloading https://files.pythonhosted.org/packages/4f/a6/728666f39bfff1719fc94c481890b2106837da9318031f71a8424b662e12/colorama-0.4.1-py2.py3-none-any.whl
Collecting s3transfer<0.3.0,>=0.2.0
  Downloading https://files.pythonhosted.org/packages/16/8a/1fc3dba0c4923c2a76e1ff0d52b305c44606da63f718d14d3231e21c51b0/s3transfer-0.2.1-py2.py3-none-any.whl (70kB)
     |????????????????????????????????| 71kB 20.5MB/s
Collecting botocore==1.13.35
  Using cached https://files.pythonhosted.org/packages/1a/69/abe9276bc3699aa89cd5f743982aa9d9ce929d7f95b4900bba60922747b0/botocore-1.13.35-py2.py3-none-any.whl
Collecting pyasn1>=0.1.3
  Downloading https://files.pythonhosted.org/packages/62/1e/a94a8d635fa3ce4cfc7f506003548d0a2447ae76fd5ca53932970fe3053f/pyasn1-0.4.8-py2.py3-none-any.whl (77kB)
     |????????????????????????????????| 81kB 23.7MB/s
Collecting futures<4.0.0,>=2.2.0; python_version == "2.6" or python_version == "2.7"
  Downloading https://files.pythonhosted.org/packages/d8/a6/f46ae3f1da0cd4361c344888f59ec2f5785e69c872e175a748ef6071cdb5/futures-3.3.0-py2-none-any.whl
Collecting jmespath<1.0.0,>=0.7.1
  Using cached https://files.pythonhosted.org/packages/83/94/7179c3832a6d45b266ddb2aac329e101367fbdb11f425f13771d27f225bb/jmespath-0.9.4-py2.py3-none-any.whl
Collecting python-dateutil<2.8.1,>=2.1; python_version >= "2.7"
  Using cached https://files.pythonhosted.org/packages/41/17/c62faccbfbd163c7f57f3844689e3a78bae1f403648a6afb1d0866d87fbb/python_dateutil-2.8.0-py2.py3-none-any.whl
Collecting urllib3<1.26,>=1.20; python_version == "2.7"
  Downloading https://files.pythonhosted.org/packages/b4/40/a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539/urllib3-1.25.7-py2.py3-none-any.whl (125kB)
     |????????????????????????????????| 133kB 89.4MB/s
Building wheels for collected packages: PyYAML
  Building wheel for PyYAML (setup.py) ... done
  Created wheel for PyYAML: filename=PyYAML-5.1.2-cp27-cp27mu-linux_x86_64.whl size=44875 sha256=d9d41338e0a62daf31574892ae853a4fbd679b5696bd62a9206522c76f83b54b
  Stored in directory: /root/.cache/pip/wheels/d9/45/dd/65f0b38450c47cf7e5312883deb97d065e030c5cca0a365030
Successfully built PyYAML
Installing collected packages: docutils, PyYAML, pyasn1, rsa, colorama, futures, jmespath, six, python-dateutil, urllib3, botocore, s3transfer, awscli
Successfully installed PyYAML-5.1.2 awscli-1.16.299 botocore-1.13.35 colorama-0.4.1 docutils-0.15.2 futures-3.3.0 jmespath-0.9.4 pyasn1-0.4.8 python-dateutil-2.8.0 rsa-3.4.2 s3transfer-0.2.1 six-1.13.0 urllib3-1.25.7
 
  • 無事、これで、aws cli が実行できるようになりました。

# aws
usage: aws [options]   [ ...] [parameters]
To see help text, you can run:
  aws help
  aws  help
  aws   help
aws: error: too few arguments