LoginSignup
2
3

More than 1 year has passed since last update.

Pipenvの仮想環境内でChaliceを使用する際に遭遇した依存関係エラー

Posted at

はじめに

今回、Chaliceで開発している他のメンバーからPipfileをもらい、自身のローカルで同様の環境を作成して、Chaliceでデプロイするにあたりエラーに遭遇したため、その解決についてまとめます。

説明しないこと

使用するリポジトリの内容

環境

  • macOS Monterey 12.3
  • AWS認証情報(アクセスキーとシークレットキー)は、ローカルに設定済み
  • インストール済み
    • Chalice 1.23
    • Python 3.8

Pipenvについて

Pipenvは、仮想環境の管理と、Pipのようなパッケージ管理の機能を持ちます。

PipfileとPipfile.lockを利用することにより、パッケージ管理を行うことができます。

  • Pipfile:要求されたパッケージの一覧などが記録されるファイル
  • Pipfile.lock:実際にインストールされるパッケージの依存関係などが記録されるファイル

npmでいうところのpackage.jsonpackage-lock.jsonと似てますね

pipenv install chaliceなど、pipenvでインストールするとPipfileに記録され、それをgitに上げて管理すると、
他のメンバーも簡単に環境を作成することができます。

Chaliceでデプロイ

chaliceのGitHubのリポジトリをクローン後、Pipfile.lockが存在するルートプロジェクト配下に移動し、
下記コマンドで、仮想環境に入ります。

% pipenv shell

そして、pipenv syncすると、Pipfile.lockの依存関係を見て、必要なライブラリをインストールしてくれます

% pipenv sync

実行すると、以下のエラーが起きました。

~省略~
[pipenv.exceptions.InstallError]: Collecting mysqlclient==2.0.3
[pipenv.exceptions.InstallError]:   Using cached mysqlclient-2.0.3.tar.gz (88 kB)
[pipenv.exceptions.InstallError]:   Preparing metadata (setup.py): started
[pipenv.exceptions.InstallError]:   Preparing metadata (setup.py): finished with status 'error'
[pipenv.exceptions.InstallError]: error: subprocess-exited-with-error
[pipenv.exceptions.InstallError]:   
[pipenv.exceptions.InstallError]:   × python setup.py egg_info did not run successfully.
[pipenv.exceptions.InstallError]:   │ exit code: 1
[pipenv.exceptions.InstallError]:   ╰─> [16 lines of output]
[pipenv.exceptions.InstallError]:       /bin/sh: mysql_config: command not found
[pipenv.exceptions.InstallError]:       /bin/sh: mariadb_config: command not found
[pipenv.exceptions.InstallError]:       /bin/sh: mysql_config: command not found
[pipenv.exceptions.InstallError]:       Traceback (most recent call last):
[pipenv.exceptions.InstallError]:         File "<string>", line 2, in <module>
[pipenv.exceptions.InstallError]:         File "<pip-setuptools-caller>", line 34, in <module>
[pipenv.exceptions.InstallError]:         File "/private/var/folders/gx/mbwfs8y566ngzlvk2j70rmr40000gn/T/pip-install-f2qo16fc/mysqlclient_fb805522744d43639e25a1e7b56aa3fa/setup.py", line 15, in <module>
[pipenv.exceptions.InstallError]:           metadata, options = get_config()
[pipenv.exceptions.InstallError]:         File "/private/var/folders/gx/mbwfs8y566ngzlvk2j70rmr40000gn/T/pip-install-f2qo16fc/mysqlclient_fb805522744d43639e25a1e7b56aa3fa/setup_posix.py", line 70, in get_config
[pipenv.exceptions.InstallError]:           libs = mysql_config("libs")
[pipenv.exceptions.InstallError]:         File "/private/var/folders/gx/mbwfs8y566ngzlvk2j70rmr40000gn/T/pip-install-f2qo16fc/mysqlclient_fb805522744d43639e25a1e7b56aa3fa/setup_posix.py", line 31, in mysql_config
[pipenv.exceptions.InstallError]:           raise OSError("{} not found".format(_mysql_config_path))
[pipenv.exceptions.InstallError]:       OSError: mysql_config not found
[pipenv.exceptions.InstallError]:       mysql_config --version
[pipenv.exceptions.InstallError]:       mariadb_config --version
[pipenv.exceptions.InstallError]:       mysql_config --libs
[pipenv.exceptions.InstallError]:       [end of output]
[pipenv.exceptions.InstallError]:   
[pipenv.exceptions.InstallError]:   note: This error originates from a subprocess, and is likely not a problem with pip.
[pipenv.exceptions.InstallError]: error: metadata-generation-failed
[pipenv.exceptions.InstallError]: 
[pipenv.exceptions.InstallError]: × Encountered error while generating package metadata.
[pipenv.exceptions.InstallError]: ╰─> See above for output.
[pipenv.exceptions.InstallError]: 
[pipenv.exceptions.InstallError]: note: This is an issue with the package mentioned above, not pip.
[pipenv.exceptions.InstallError]: hint: See above for details.

Pipfile.lockにmysqlclientが記載しているので、mysqlclientモジュールをインストールしたいのですが、
上記のエラーは、mysql_configが見つからないと記載があります。

/bin/sh: mysql_config: command not found
/bin/sh: mariadb_config: command not found

そして、OSエラーとあるので、ローカル(今回はMacOS)にMySQLがインストールされていないことが原因と推測できます。

OSError: mysql_config not found

そのため、HomebrewでMySQLをインストールするとよいです。

仮想環境内から出てからインストールしましょう。

% brew install mysql

% type -a mysql_config
mysql_config is /usr/local/bin/mysql_config

mysql_configの存在を確認することができました。

それでは、Chaliceをデプロイするため、appディレクトリ配下に移動し、デプロイしてみましょう。

% pipenv shell

% cd app 
% chalice deploy --stage dev

Traceback (most recent call last):

~省略~
    raise RuntimeError("Unknown import string for pip version: %s"
RuntimeError: Unknown import string for pip version: (22, 2)

pipのバージョンが22.2の場合、Chaliceが動作しないようです。下記の記事を見ると、pipのバージョンが22.0.0は、使えるようです。

pipを22.0.0にダウングレードしましょう。

 % pip install --upgrade pip==22.0.0
Collecting pip==22.0.0
  Downloading pip-22.0-py3-none-any.whl (2.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 6.4 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.2.2
    Uninstalling pip-22.2.2:
      Successfully uninstalled pip-22.2.2
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
chalice 1.23.0 requires pip<21.2,>=9, but you have pip 22.0 which is incompatible.
Successfully installed pip-22.0

Pipfile.lockのChaliceは、1.23.0ですが、pipのバージョンが22.0は依存関係にないようです。

pipのバージョンが21.1に下げてみましょう。

% pip install --upgrade pip==21.1               
Collecting pip==21.1
  Downloading pip-21.1-py3-none-any.whl (1.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 5.7 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.0
    Uninstalling pip-22.0:
      Successfully uninstalled pip-22.0
Successfully installed pip-21.1

成功しました。それでは、chaliceをデプロイしてみましょう。

% chalice deploy --stage dev

問題なくデプロイできました。

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3