LoginSignup
2
3

More than 3 years have passed since last update.

pg_configとかビルドとかwheelとかよくわからないけどMacにpsycopg2を入れたい

Last updated at Posted at 2020-08-19

DjangoでPostgreSQLを使用したいなと思い、
普通にpip install psycopg2でインストールしたいなと思った時に、
いろいろ詰まって調べていた中に体系的な情報が少なかったので、
備忘録的に書き溜めておきます。

macOS Catalina 10.15.6
Python 3.8.5
pip 20.1.1

普通にpip installした時に、長文の赤い文字のエラー文が吐き出されてこちらまで吐血しそうだったのですが、エラー文を掻い摘んで見てみると。

Using cached psycopg2-2.8.5.tar.gz (380 kB)
Building wheels for collected packages: psycopg2
Building wheel for psycopg2 (setup.py) ... error

wheelのBuilding??ナンノコッチャ??
とりあえずsetuptoolsとwheelというのをインストールしてみる。

% pip install --upgrade pip setuptools wheel

Requirement already up-to-date: pip in ./myenv/lib/python3.8/site-packages (20.2.2)
Collecting setuptools
  Using cached setuptools-49.6.0-py3-none-any.whl (803 kB)
Requirement already up-to-date: wheel in ./myenv/lib/python3.8/site-packages (0.35.1)
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 47.1.0
    Uninstalling setuptools-47.1.0:
      Successfully uninstalled setuptools-47.1.0
Successfully installed setuptools-49.6.0

結果は変わらなかった。
もっとよく見てみるとこんな親切なドキュメントを見つけた。

It appears you are missing some prerequisite to build the package from source.

    You may install a binary package by installing 'psycopg2-binary' from PyPI.
    If you want to install psycopg2 from source, please install the packages
    required for the build and try again.

    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).

「アンタの場合、psycopg2-binaryというのをPyPIからとったきた方がいんじゃない?
でもどうしてもpsycopg2をインストールしたいんなら、ビルドに必要なパッケージをインストールしてやりなおしてみたら??」とのこと。

親切にURLも貼ってくれているので見に行ってみることに。
https://www.psycopg.org/docs/install.html

psycopg2PyPIでは、最も一般的なプラットフォーム(Linux、OSX、Windows)のホイールパッケージの形式でも利用できます。これにより、上記のビルドまたはランタイムの前提条件を必要とせずに、モジュールのバイナリバージョンをインストールできるようになります。

psycopg2-binaryでいけそう。

 % pip install psycopg2-binary

Collecting psycopg2-binary
  Downloading psycopg2_binary-2.8.5-cp38-cp38-macosx_10_9_x86_64.macosx_10_9_intel.macosx_10_10_intel.macosx_10_10_x86_64.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 5.8 MB/s 
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.8.5

おっいけた!
importしてみよう!

>>> import psycopg2
>>> 

やったぜ!!

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