[Python] 在 Mac 上安裝存取 PostgreSQL 用的 psycopg2 套件

[Python] 在 Mac 上安裝存取 PostgreSQL 用的 psycopg2 套件

今天想在 Mac 上安裝 Python 套件 psycopg2 (用來存取 PostgreSQL) 時,

出現了錯誤訊息:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ pip install psycopg2
Looking in indexes: https://pypi.org/simple, https://:****@jfrog.trendmicro.com/artifactory/api/pypi/tcp-python-sdk-local/simple
Collecting psycopg2
Downloading psycopg2-2.9.10.tar.gz (385 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [21 lines of output]
running egg_info
writing psycopg2.egg-info/PKG-INFO
writing dependency_links to psycopg2.egg-info/dependency_links.txt
writing top-level names to psycopg2.egg-info/top_level.txt
Error: pg_config executable not found.
pg_config is required to build psycopg2 from source. Please add the directory
containing pg_config to the $PATH or specify the full executable path with the
option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.
For further information please check the 'doc/src/install.rst' file (also at
<https://www.psycopg.org/docs/install.html>).
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
$ pip install psycopg2 Looking in indexes: https://pypi.org/simple, https://:****@jfrog.trendmicro.com/artifactory/api/pypi/tcp-python-sdk-local/simple Collecting psycopg2 Downloading psycopg2-2.9.10.tar.gz (385 kB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [21 lines of output] running egg_info writing psycopg2.egg-info/PKG-INFO writing dependency_links to psycopg2.egg-info/dependency_links.txt writing top-level names to psycopg2.egg-info/top_level.txt Error: pg_config executable not found. pg_config is required to build psycopg2 from source. Please add the directory containing pg_config to the $PATH or specify the full executable path with the option: python setup.py build_ext --pg-config /path/to/pg_config build ... or with the pg_config option in 'setup.cfg'. If you prefer to avoid building psycopg2 from source, please install the PyPI 'psycopg2-binary' package instead. For further information please check the 'doc/src/install.rst' file (also at <https://www.psycopg.org/docs/install.html>). [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
$ pip install psycopg2

Looking in indexes: https://pypi.org/simple, https://:****@jfrog.trendmicro.com/artifactory/api/pypi/tcp-python-sdk-local/simple
Collecting psycopg2
  Downloading psycopg2-2.9.10.tar.gz (385 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [21 lines of output]
      running egg_info
      writing psycopg2.egg-info/PKG-INFO
      writing dependency_links to psycopg2.egg-info/dependency_links.txt
      writing top-level names to psycopg2.egg-info/top_level.txt

      Error: pg_config executable not found.

      pg_config is required to build psycopg2 from source.  Please add the directory
      containing pg_config to the $PATH or specify the full executable path with the
      option:

          python setup.py build_ext --pg-config /path/to/pg_config build ...

      or with the pg_config option in 'setup.cfg'.

      If you prefer to avoid building psycopg2 from source, please install the PyPI
      'psycopg2-binary' package instead.

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

      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

 

看起來是因為沒有 pg_config…

不過錯誤訊息也說如果你不打算從原始碼開始編譯的話,

可以用 psycopg2-binary 這個套件:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ pip install psycopg2-binary
Looking in indexes: https://pypi.org/simple, https://:****@jfrog.trendmicro.com/artifactory/api/pypi/tcp-python-sdk-local/simple
Collecting psycopg2-binary
Downloading psycopg2_binary-2.9.10-cp311-cp311-macosx_14_0_arm64.whl (3.3 MB)
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.9.10
$ pip install psycopg2-binary Looking in indexes: https://pypi.org/simple, https://:****@jfrog.trendmicro.com/artifactory/api/pypi/tcp-python-sdk-local/simple Collecting psycopg2-binary Downloading psycopg2_binary-2.9.10-cp311-cp311-macosx_14_0_arm64.whl (3.3 MB) Installing collected packages: psycopg2-binary Successfully installed psycopg2-binary-2.9.10
$ pip install psycopg2-binary

Looking in indexes: https://pypi.org/simple, https://:****@jfrog.trendmicro.com/artifactory/api/pypi/tcp-python-sdk-local/simple
Collecting psycopg2-binary
Downloading psycopg2_binary-2.9.10-cp311-cp311-macosx_14_0_arm64.whl (3.3 MB)
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.9.10

 

看來也是可以正常使用的喔:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ python
Python 3.11.9 (main, Aug 12 2024, 18:16:47) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.9.10 (dt dec pq3 ext lo64)'
$ python Python 3.11.9 (main, Aug 12 2024, 18:16:47) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import psycopg2 >>> psycopg2.__version__ '2.9.10 (dt dec pq3 ext lo64)'
$ python

Python 3.11.9 (main, Aug 12 2024, 18:16:47) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import psycopg2

>>> psycopg2.__version__
'2.9.10 (dt dec pq3 ext lo64)'

 

(本頁面已被瀏覽過 55 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料