最近、pip や pipenv のコマンドを使う機会が増えてきたので pip コマンドについてまとめてみました。
pip は依存関係に引っ掛かることも多いですが、ここは慣れですね。
ヘルプ
ヘルプは –help か -h になります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ pip --help Usage: pip <command> [options] Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. check Verify installed packages have compatible dependencies. config Manage local and global configuration. search Search PyPI for packages. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion. help Show help for commands. (一部省略) |
バージョン確認
バージョンは –version か -V(大文字のV)で確認できます。
手元の EC2 の AmazonLinux は python2.7 だったのか・・・。
1 2 | $ pip --version pip 9.0.3 from /usr/lib/python2.7/dist-packages (python 2.7) |
インストール済みのパッケージ一覧
以前、Redis のモジュールがインストールされていなくて import でエラーになった苦い記憶があるので、インストール済のパッケージは事前に確認しておきます。
1 2 3 4 | $ pip list (途中省略) You are using pip version 9.0.3, however version 18.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. |
現在は 18.1 が最新だからアップグレードしてねっと丁寧に指摘されました。
インストール済パッケージの確認の前にバージョンアップしておきますか。
1 2 3 4 5 6 7 8 | $ pip install --upgrade pip Collecting pip Downloading xxxxxxxxxx Installing collected packages: pip Found existing installation: pip 9.0.3 Uninstalling pip-9.0.3: Successfully uninstalled pip-9.0.3 Successfully installed pip-18.1 |
無事に最新になりました。
1 2 | $ pip --version pip 18.1 from /usr/lib/python2.7/dist-packages/pip (python 2.7) |
再度、インストール済のパッケージ一覧を確認します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | $ pip list Package Version ---------------------------- --------- aws-cfn-bootstrap 1.4 awscli 1.15.83 Babel 0.9.4 backports.ssl-match-hostname 3.4.0.2 boto 2.48.0 botocore 1.10.82 certifi 2018.8.13 chardet 3.0.4 click 6.7 cloud-init 0.7.6 colorama 0.2.5 configobj 4.7.2 docutils 0.11 ecdsa 0.11 Flask 1.0.2 futures 3.0.3 gevent 1.3.6 greenlet 0.4.14 hibagent 1.0.0 idna 2.7 iniparse 0.3.1 itsdangerous 0.24 Jinja2 2.10 jmespath 0.9.2 jsonpatch 1.2 jsonpointer 1.0 kitchen 1.1.1 lockfile 0.8 locustio 0.8.1 MarkupSafe 1.0 msgpack-python 0.5.6 paramiko 1.15.1 PIL 1.1.6 pip 18.1 ply 3.4 pyasn1 0.1.7 pycrypto 2.6.1 pycurl 7.19.0 pygpgme 0.3 pyliblzma 0.5.3 pystache 0.5.3 python-daemon 1.5.2 python-dateutil 2.1 pyxattr 0.5.0 PyYAML 3.10 pyzmq 17.1.2 requests 2.19.1 rsa 3.4.1 setuptools 36.2.7 simplejson 3.6.5 six 1.11.0 urlgrabber 3.10 urllib3 1.23 virtualenv 15.1.0 Werkzeug 0.14.1 yum-metadata-parser 1.1.4 |
コマンド履歴を確認したら、過去に locustio をインストールした形跡があったので、それに依存するパッケージが標準パッケージに追加された感じですね。
locustio をインストールした時の記事はこちら。

インストール可能なパッケージ検索
BeautifulSoup が入っていないので、インストール可能な BeautifulSoup のパッケージを探してみます。
1 2 3 4 5 | $ pip search beautifulsoup ipython-beautifulsoup (0.3) - Custom rendering of beautifulsoup objects in IPython notebook and qtconsole scrapy-beautifulsoup (0.0.2) - Simple Scrapy middleware to process non-well-formed HTML with BeautifulSoup BeautifulSoup (3.2.1) - HTML/XML parser for quick-turnaround applications like screen-scraping. (一部省略) |
BeautifulSoup が 4 ではなく 3.2.1 なのは python2.7 だからなのかな。
パッケージをインストール
BeautifulSoup のパッケージをインストールしてみます。
1 2 3 4 5 6 | $ pip install BeautifulSoup Collecting BeautifulSoup Downloading xxxxxxxxxx Installing collected packages: BeautifulSoup Running setup.py install for BeautifulSoup ... done Successfully installed BeautifulSoup-3.2.1 |
パッケージ一覧を確認します。
1 2 | $ pip list | grep 'BeautifulSoup' BeautifulSoup 3.2.1 |
最新じゃないパッケージ一覧
冒頭で pip のバージョンアップを行いましたが、他のインストール済パッケージの最新バージョンを確認してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | $ pip list --outdated Package Version Latest Type ---------------------------- --------- ---------- ----- awscli 1.15.83 1.16.51 wheel Babel 0.9.4 2.6.0 wheel backports.ssl-match-hostname 3.4.0.2 3.5.0.1 sdist boto 2.48.0 2.49.0 wheel botocore 1.10.82 1.12.41 wheel certifi 2018.8.13 2018.10.15 wheel click 6.7 7.0 wheel colorama 0.2.5 0.4.0 wheel configobj 4.7.2 5.0.6 sdist docutils 0.11 0.14 wheel ecdsa 0.11 0.13 wheel futures 3.0.3 3.2.0 wheel gevent 1.3.6 1.3.7 wheel greenlet 0.4.14 0.4.15 wheel iniparse 0.3.1 0.4 sdist itsdangerous 0.24 1.1.0 wheel jmespath 0.9.2 0.9.3 wheel jsonpatch 1.2 1.23 wheel jsonpointer 1.0 2.0 wheel kitchen 1.1.1 1.2.5 sdist lockfile 0.8 0.12.2 wheel locustio 0.8.1 0.9.0 wheel MarkupSafe 1.0 1.1.0 wheel paramiko 1.15.1 2.4.2 wheel ply 3.4 3.11 wheel pyasn1 0.1.7 0.4.4 wheel pycurl 7.19.0 7.43.0.2 sdist pystache 0.5.3 0.5.4 sdist python-daemon 1.5.2 2.2.0 sdist python-dateutil 2.1 2.7.5 wheel pyxattr 0.5.0 0.6.1 sdist PyYAML 3.10 3.13 sdist requests 2.19.1 2.20.1 wheel rsa 3.4.1 4.0 wheel setuptools 36.2.7 40.5.0 wheel simplejson 3.6.5 3.16.0 sdist urlgrabber 3.10 3.10.2 sdist urllib3 1.23 1.24.1 wheel virtualenv 15.1.0 16.1.0 wheel |
めっちゃ出ましたね・・・。
これらを最新にしておきたいですが、冒頭で紹介したヘルプの結果に update や upgrade のオプションは記載されていませんでした。
パッケージの更新(アップデート)
pip のバージョンアップを催促された時に –upgrade を指定されたのでこれを使ってみます。
1 2 3 4 5 6 7 8 | $ pip install --upgrade awscli Installing collected packages: botocore, s3transfer, awscli Found existing installation: botocore 1.10.82 Uninstalling botocore-1.10.82: Successfully uninstalled botocore-1.10.82 Found existing installation: awscli 1.15.83 Uninstalling awscli-1.15.83: Successfully uninstalled awscli-1.15.83 |
1 つずつ指定するのは面倒なのでパッケージを省略してみます。
1 2 | $ pip install --upgrade ERROR: You must give at least one requirement to install (see "pip help install") |
なるほど。インストールコマンド用のヘルプがあるんですね。
1 2 3 4 5 6 7 8 9 10 11 12 | $ pip help install Usage: pip install [options] <requirement specifier> [package-index-options] ... pip install [options] -r <requirements file> [package-index-options] ... pip install [options] [-e] <vcs project url> ... pip install [options] [-e] <local project path> ... pip install [options] <archive url/path> ... (一部省略) Install Options: -U, --upgrade Upgrade all specified packages to the newest available version. The handling of dependencies depends on the upgrade-strategy used. (一部省略) |
ワイルドカード指定とかできないでしょうか。
1 2 3 4 | $ pip install --upgrade * Collecting a.txt Could not find a version that satisfies the requirement a.txt (from versions: ) No matching distribution found for a.txt |
無理でしたね・・・。
どうやらテキストファイルなどにパッケージ一覧を記載したものを指定するしか方法がないようなので、先ほどのアップグレード対象のパッケージを出力します。
1 2 3 4 5 6 | $ pip list --outdated | awk '{print $1}' Package ---------------------------- Babel backports.ssl-match-hostname boto |
ヘッダの 2 行が邪魔ですね。
awk で指定の行数以降だけを取得する場合は NR が利用できます。
列は NF でしたが、行は NR なんですね。

1 2 3 4 | $ pip list --outdated | awk 'NR>2{print $1}' Babel backports.ssl-match-hostname boto |
これでをファイル出力して読み込ませてもいいのですが、どうせならこのまま一気にアップデートしちゃいましょう。
あとは依存関係などのエラーが出ないことを祈りつつ・・・。
1 | $ pip list --outdated | awk 'NR>2{print $1}' | xargs pip install --upgrade |
インストールやアップグレード時のエラー
上記で祈った甲斐もなくエラーが出ました。
pip コマンドを使う時は何かしら警告やエラーに遭遇する気がします。
どうしても必要なモジュールの場合は全力で解決に向かいますが、今回のように何気なく最新アップデートした場合などは時間に余裕がある時に解決しましょう。
意外にハマりますから・・・。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Collecting pycurl Using cached https://files.pythonhosted.org/packages/e8/e4/0dbb8735407189f00b33d84122b9be52c790c7c3b25286826f4e1bdb7bde/pycurl-7.43.0.2.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-DtJljr/pycurl/setup.py", line 913, in <module> ext = get_extension(sys.argv, split_extension_source=split_extension_source) File "/tmp/pip-install-DtJljr/pycurl/setup.py", line 582, in get_extension ext_config = ExtensionConfiguration(argv) File "/tmp/pip-install-DtJljr/pycurl/setup.py", line 99, in __init__ self.configure() File "/tmp/pip-install-DtJljr/pycurl/setup.py", line 227, in configure_unix raise ConfigurationError(msg) __main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory |
どうやら libcurl が必要らしいですね。
1 2 3 4 5 6 7 8 9 10 | $ yum search curl collectd-curl.x86_64 : Curl plugin for collectd collectd-curl_xml.x86_64 : Curl XML plugin for collectd libcurl-devel.x86_64 : Files needed for building applications with libcurl perl-WWW-Curl.x86_64 : Perl extension interface for libcurl python26-pycurl.x86_64 : A Python interface to libcurl python27-pycurl.x86_64 : A Python interface to libcurl curl.x86_64 : A utility for getting files from remote servers (FTP, HTTP, and others) libcurl.i686 : A library for getting files from web servers libcurl.x86_64 : A library for getting files from web servers |
libcurl-devel をインストールします。
1 | $ yum -y install libcurl-devel |
そして、再度アップデート。
1 | $ pip list --outdated | awk 'NR>2{print $1}' | xargs pip install --upgrade |
むぅ・・・。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Collecting pycurl Using cached https://files.pythonhosted.org/packages/e8/e4/0dbb8735407189f00b33d84122b9be52c790c7c3b25286826f4e1bdb7bde/pycurl-7.43.0.2.tar.gz Complete output from command python setup.py egg_info: Using curl-config (libcurl 7.53.1) Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-bdcnVN/pycurl/setup.py", line 913, in <module> ext = get_extension(sys.argv, split_extension_source=split_extension_source) File "/tmp/pip-install-bdcnVN/pycurl/setup.py", line 582, in get_extension ext_config = ExtensionConfiguration(argv) File "/tmp/pip-install-bdcnVN/pycurl/setup.py", line 99, in __init__ self.configure() File "/tmp/pip-install-bdcnVN/pycurl/setup.py", line 316, in configure_unix specify the SSL backend manually.''') __main__.ConfigurationError: Curl is configured to use SSL, but we have not been able to determine which SSL backend it is using. Please see PycURL documentation for how to specify the SSL backend manually. |
どうやら、SSL に対応した pycurl が必要な模様。
1 2 | $ export PYCURL_SSL_LIBRARY=openssl $ pip list --outdated | awk 'NR>2{print $1}' | xargs pip install --upgrade |
pycurl の SSL は上記で通過しましたが、今度は依存関係のエラーが出ちゃいました。
無理に全部のモジュールを最新にすることもないなってことで、この記事はここまでで。
1 | awscli 1.16.51 has requirement colorama<=0.3.9,>=0.2.5, but you'll have colorama 0.4.0 which is incompatible. |
気が向いたら解決というか、今後は python3.6 か 3.7 を使います。