Installing Psycopg2 with "pip" in Python
When using Python with virtual environments to install the database driver psycopg2, users may encounter errors related to the absence of the pg_config executable. To resolve this issue, additional steps are necessary.
Error Message:
Error: pg_config executable not found. 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'.
Solution:
There are two main approaches to install psycopg2 successfully:
Option 1: Install Binary Wheels
For users on Windows, it is recommended to install the psycopg2-binary package, which provides pre-built binaries for various Python versions. This simplifies the installation process:
pip install psycopg2-binary
Option 2: Build from Source (Linux/Mac)
Prerequisites:
To build psycopg2 from source, install the following dependencies:
Debian/Ubuntu:
Steps:
Download the psycopg2 source code:
wget https://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.8.6.tar.gz
Extract the archive:
tar xvf psycopg2-2.8.6.tar.gz
Navigate to the extracted directory:
cd psycopg2-2.8.6
Configure the build by passing the path to the pg_config executable:
python setup.py build_ext --pg-config /path/to/pg_config
Install the package:
sudo python setup.py install
Verify the installation:
python -c "import psycopg2; print(psycopg2.__version__)"
Note:
The above is the detailed content of How to Fix pg_config Executable Not Found Error When Installing Psycopg2 with \'pip\' in Python?. For more information, please follow other related articles on the PHP Chinese website!