如何用pg_config错误安装psycopg2?

时间:2016-01-30 16:25:02

标签: python postgresql psycopg2

我尝试从docs安装psycopg2(PostgreSQL数据库适配器),但是当我尝试安装后,我进入包并编写

Field1.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() == 4)
      // length is 4 characters do what you want to do here.
      Toast.makeText(context, s, 100).show();
   }
  });

我收到以下错误:

python setup.py install 

我也试过'sudo pip install psycopg2',我收到了同样的消息。

阅读enter image description here后,它要求查看setup.cfg文件(如下所示):

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'.

但是,我不确定我是否要编辑此文件,因为文档说明了以下内容:

[build_ext]
define=

# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information

# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
#pg_config=

# Set to 1 to use Python datetime objects for default date/time representation.
use_pydatetime=1

# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=

# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0

# Statically link against the postgresql client library.
#static_libpq=1

# Add here eventual extra libraries required to link the module.
#libraries=

我已经获得了支持的选项列表,但我不确定从那里开始

8 个答案:

答案 0 :(得分:4)

Debian/Ubuntu

Python 2

sudo apt install libpq-dev python-dev

Python 3

sudo apt install libpq-dev python3-dev

其他

If none of the above solve your issue, try

sudo apt install build-essential
or

sudo apt install postgresql-server-dev-all

答案 1 :(得分:3)

如果你在Ubuntu上试试

sudo apt-get install python-psycopg2

否则,您需要为您的发行版查找并安装Postgresql客户端软件包。 psycopg2 installation from source

答案 2 :(得分:1)

升级pip对我有用: pip install --upgrade pip

答案 3 :(得分:1)

我遇到了这个问题,因为我尚未在机器上安装PostgreSQL。在Mac上,只需一个简单的brew install postgresql就可以解决问题。

答案 4 :(得分:0)

对于像我这样的来源构建postgrespsycopg2的人,其他解决方案就在这里:

sudo su
export PATH=/usr/local/pgsql/bin:$PATH #or path to your pg_config

现在来自psycopg2的setup.py可以正确找到pg_config。

python3 setup.py install

或者如果您只想使用pip3pip3 install psycopg2也可以使用。

答案 5 :(得分:0)

如果您需要安装而无需编译:

pip install psycopg2-binary

答案 6 :(得分:0)

对于带有Macports的OSX,您可以为Python 3.8安装sudo port install py38-psycopg2。您可以使用port search psycopg2搜索您的Python版本。在撰写本文时,该版本的范围从2.7到3.9一直是psycopg2的2.8.6的最新版本。

注意:这可能对通气没有帮助。

编辑:因此,要查找venv的pg_config,请运行命令port contents postgresql13 | grep pg_config,其中postgresql13是从上述port命令安装的postgresql软件包的版本。您正在寻找诸如/opt/local/lib/postgresql13/bin/pg_config之类的路径。使用诸如export PATH=/opt/local/lib/postgresql13/bin/:$PATH之类的命令将其导出到您的PATH变量。

请参阅this answer,了解自制程序。

答案 7 :(得分:0)

在配备 M1 的 Macbook 上,我必须安装 postgresql

brew install postgresql

(如果您没有 brew:https://brew.sh

然后再次运行安装

python3 -m pip install psycopg2-binary