问题:如何在Python上使用“ pip”安装psycopg2?

我在用着 virtualenv,我需要安装“ psycopg2”。

我已经完成以下工作:

pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160

我有以下消息:

Downloading/unpacking http://pypi.python.org/packages/source/p/psycopg2/psycopg2
-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
  Downloading psycopg2-2.4.tar.gz (607Kb): 607Kb downloaded
  Running setup.py egg_info for package from http://pypi.python.org/packages/sou
rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
    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'.
    Complete output from command python setup.py egg_info:
    running egg_info

creating pip-egg-info\psycopg2.egg-info

writing pip-egg-info\psycopg2.egg-info\PKG-INFO

writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt

writing dependency_links to pip-egg-info\psycopg2.egg-info\dependency_links.txt

writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.txt'

warning: manifest_maker: standard file '-c' not found

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

----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in C:\Documents and Settings\anlopes\Application Data\pip\p
ip.log

我的问题是,我只需要这样做才能使psycopg2工作?

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

I’m using virtualenv and I need to install “psycopg2”.

I have done the following:

pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160

And I have the following messages:

Downloading/unpacking http://pypi.python.org/packages/source/p/psycopg2/psycopg2
-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
  Downloading psycopg2-2.4.tar.gz (607Kb): 607Kb downloaded
  Running setup.py egg_info for package from http://pypi.python.org/packages/sou
rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
    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'.
    Complete output from command python setup.py egg_info:
    running egg_info

creating pip-egg-info\psycopg2.egg-info

writing pip-egg-info\psycopg2.egg-info\PKG-INFO

writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt

writing dependency_links to pip-egg-info\psycopg2.egg-info\dependency_links.txt

writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.txt'

warning: manifest_maker: standard file '-c' not found

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

----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in C:\Documents and Settings\anlopes\Application Data\pip\p
ip.log

My question, I only need to do this to get the psycopg2 working?

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

回答 0

注意:早在不久之前,PyPI中就存在Windows的二进制轮子,因此Windows用户不再是问题。以下是适用于Linux和Mac用户的解决方案,因为他们中的很多人都是通过网络搜索找到这篇文章的。


选项1

psycopg2-binary而是安装PyPI软件包,它具有适用于Linux和Mac OS的Python轮子。

pip install psycopg2-binary

选项2

安装先决条件来构建 psycopg2从源代码软件包:

Debian / Ubuntu

Python 3

sudo apt install libpq-dev python3-dev

您可能需要安装 python3.8-dev或类似的工具,例如Python 3.8。

Python 2 1

sudo apt install libpq-dev python-dev

如果还不够,请尝试

sudo apt install build-essential

要么

sudo apt install postgresql-server-dev-all

在再次安装psycopg2之前。

CentOS的6

参见班杰的答案


1真的吗?2020年

Note: Since a while back, there are binary wheels for Windows in PyPI, so this should no longer be an issue for Windows users. Below are solutions for Linux, Mac users, since lots of them find this post through web searches.


Option 1

Install the psycopg2-binary PyPI package instead, it has Python wheels for Linux and Mac OS.

pip install psycopg2-binary

Option 2

Install the prerequsisites for building the psycopg2 package from source:

Debian/Ubuntu

Python 3

sudo apt install libpq-dev python3-dev

You might need to install python3.8-dev or similar for e.g. Python 3.8.

Python 21

sudo apt install libpq-dev python-dev

If that’s not enough, try

sudo apt install build-essential

or

sudo apt install postgresql-server-dev-all

as well before installing psycopg2 again.

CentOS 6

See Banjer’s answer


1 Really? It’s 2020


回答 1

在CentOS上,您需要postgres开发软件包:

sudo yum install python-devel postgresql-devel

至少这是CentOS 6上的解决方案。

On CentOS, you need the postgres dev packages:

sudo yum install python-devel postgresql-devel

That was the solution on CentOS 6 at least.


回答 2

在安装了Postgres.app 9.3.2.0 RC2的Mac Mavericks上,安装Postgres之后,我需要使用以下代码:

sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin pip install psycopg2

On Mac Mavericks with Postgres.app version 9.3.2.0 RC2 I needed to use the following code after installing Postgres:

sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin pip install psycopg2


回答 3

如果您使用的是Mac,则可以使用自制软件

brew install postgresql

其他所有选项都在这里:http : //www.postgresql.org/download/macosx/

祝好运

if you’re on a mac you can use homebrew

brew install postgresql

And all other options are here: http://www.postgresql.org/download/macosx/

Good luck


回答 4

我最近在Windows机器上配置了psycopg2。最简单的安装是使用Windows可执行二进制文件。您可以在http://stickpeople.com/projects/python/win-psycopg/中找到它。

要在虚拟环境中安装本机二进制文件,请使用easy_install:

C:\virtualenv\Scripts\> activate.bat
(virtualenv) C:\virtualenv\Scripts\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe

I recently configured psycopg2 on a windows machine. The easiest install is using a windows executable binary. You can find it at http://stickpeople.com/projects/python/win-psycopg/.

To install the native binary in a virtual envrionment, use easy_install:

C:\virtualenv\Scripts\> activate.bat
(virtualenv) C:\virtualenv\Scripts\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe

回答 5

对于Python 3,您应该sudo apt-get install libpq-dev python3-dev在Debian下使用。

For Python 3 you should use sudo apt-get install libpq-dev python3-dev under Debian.


回答 6

这对我有用(在RHEL,CentOS上:

sudo yum install postgresql postgresql-devel python-devel

现在在pip install中包含指向您的postgresql二进制目录的路径:

sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2

确保包括正确的路径。就这样 :)

更新:对于python 3,请安装python3-devel而不是python-devel

This is what worked for me (On RHEL, CentOS:

sudo yum install postgresql postgresql-devel python-devel

And now include the path to your postgresql binary dir with you pip install:

sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2

Make sure to include the correct path. Thats all :)

UPDATE: For python 3, please install python3-devel instead of python-devel


回答 7

如果使用Mac OS,则应从源代码安装PostgreSQL。安装完成后,您需要使用以下方法添加此路径:

export PATH=/local/pgsql/bin:$PATH

或者您可以像这样添加路径:

export PATH=.../:usr/local/pgsql/bin

在您的.profile文件或.zshrc文件中。

这可能因操作系统而异。

您可以从http://www.thegeekstuff.com/2009/04/linux-postgresql-install-and-configure-from-source/遵循安装过程

If you using Mac OS, you should install PostgreSQL from source. After installation is finished, you need to add this path using:

export PATH=/local/pgsql/bin:$PATH

or you can append the path like this:

export PATH=.../:usr/local/pgsql/bin

in your .profile file or .zshrc file.

This maybe vary by operating system.

You can follow the installation process from http://www.thegeekstuff.com/2009/04/linux-postgresql-install-and-configure-from-source/


回答 8

到目前为止的答案太像魔术食谱。收到的错误告诉您pip无法找到PostgreSQL查询库的所需部分。可能是因为您在操作系统的非标准位置安装了它,这就是为什么该消息建议使用–pg-config选项的原因。

但是更常见的原因是您根本没有安装libpq。这通常发生在没有安装PostgreSQL服务器的机器上,因为您只想运行客户端应用程序,而不是服务器本身。每个OS /发行版都不相同,例如在Debian / Ubuntu上,您需要安装libpq-dev。这使您可以针对PostgreSQL查询库编译和链接代码。

大多数答案还建议安装Python开发库。小心。如果仅使用发行版中安装的默认Python,则可以使用,但是如果您使用的是较新版本,则可能会引起问题。如果您在此计算机上构建了Python,那么您已经具有编译C / C ++库与Python交互所需的dev库。只要您使用的是正确的pip版本,即与python二进制文件安装在同一bin文件夹中的版本,您便都已准备就绪。无需安装旧版本。

The answers so far are too much like magic recipes. The error that you received tells you that pip cannot find a needed part of the PostgreSQL Query library. Possibly this is because you have it installed in a non-standard place for your OS which is why the message suggests using the –pg-config option.

But a more common reason is that you don’t have libpq installed at all. This commonly happens on machines where you do NOT have PostgreSQL server installed because you only want to run client apps, not the server itself. Each OS/distro is different, for instance on Debian/Ubuntu you need to install libpq-dev. This allows you to compile and link code against the PostgreSQL Query library.

Most of the answers also suggest installing a Python dev library. Be careful. If you are only using the default Python installed by your distro, that will work, but if you have a newer version, it could cause problems. If you have built Python on this machine then you already have the dev libraries needed for compiling C/C++ libraries to interface with Python. As long as you are using the correct pip version, the one installed in the same bin folder as the python binary, then you are all set. No need to install the old version.


回答 9

Debian/Ubuntu

首先安装和构建psycopg2软件包的依赖项:

# apt-get build-dep python-psycopg2

然后在您的虚拟环境中,编译并安装psycopg2模块:

(env)$ pip install psycopg2

On Debian/Ubuntu:

First install and build dependencies of psycopg2 package:

# apt-get build-dep python-psycopg2

Then in your virtual environment, compile and install psycopg2 module:

(env)$ pip install psycopg2

回答 10

我已经在Windows中首先安装到基本python安装中的位置之前完成了此操作。

然后,您手动将已安装的psycopg2复制到virtualenv安装中。

它不漂亮,但是可以用。

I’ve done this before where in windows you install first into your base python installation.

Then, you manually copy the installed psycopg2 to the virtualenv install.

It’s not pretty, but it works.


回答 11

除了安装必需的软件包外,我还需要手动将PostgreSQL bin目录添加到PATH。在之前
$vi ~/.bash_profile
添加。PATH=/usr/pgsql-9.2/bin:$PATHexport PATH
$source ~/.bash_profile
$pip install psycopg2

Besides installing the required packages, I also needed to manually add PostgreSQL bin directory to PATH.
$vi ~/.bash_profile
Add PATH=/usr/pgsql-9.2/bin:$PATH before export PATH.
$source ~/.bash_profile
$pip install psycopg2


回答 12

在Windows XP上,如果未安装postgres,则会出现此错误…

On windows XP you get this error if postgres is not installed …


回答 13

我使用PG下载网站http://www.postgresql.org/download/linux/redhat/上的RedHat / CentOS存储库安装了Postgresql92

要获取pg_config,我必须将/usr/pgsql-9.2/bin添加到PATH。

I installed Postgresql92 using the RedHat / CentOS repository on PG’s downloads site http://www.postgresql.org/download/linux/redhat/

To get pg_config, I had to add /usr/pgsql-9.2/bin to PATH.


回答 14

在安装psycopg2之前,您需要安装python-dev软件包。

如果您使用的是Linux(可能还有其他系统,但我不能从经验上讲),则在安装dev软件包时,需要确保准确地了解运行的python版本。

例如,当我使用命令时:

sudo apt-get install python3-dev

尝试执行以下操作时,我仍然遇到相同的错误

pip install psycopg2

当我使用python 3.7时,我需要使用命令

sudo apt-get install python3.7-dev

完成此操作后,我再也不会遇到任何问题。显然,如果您使用的是python 3.5版,则可以将该7更改为5。

Before you can install psycopg2 you will need to install the python-dev package.

If you’re working from Linux (and possibly other systems but i can’t speak from experience) you will need to make sure to be quite exact about what version of python your running when installing the dev package.

For example when I used the command:

sudo apt-get install python3-dev

I still ran into the same error when trying to

pip install psycopg2

As I am using python 3.7 I needed to use the command

sudo apt-get install python3.7-dev

Once I did this I ran into no more issues. Obviously if your on python version 3.5 you would change that 7 to a 5.


回答 15

我已经为此奋斗了好几天,终于找到了如何使“ pip install psycopg2”命令在Windows(运行Cygwin)的virtualenv中运行的方法。

我碰到了“找不到pg_config可执行文件”。错误,但我已经在Windows中下载并安装了postgres。它也安装在Cygwin中。在Cygwin中运行“哪个pg_config”给出了“ / usr / bin / pg_config”,而运行“ pg_config”给出了合理的输出-但是,与Cygwin一起安装的版本是:

版本= PostgreSQL 8.2.11

这不适用于当前版本的psycopg2,后者似乎至少需要9.1。当我在Windows路径中添加“ c:\ Program Files \ PostgreSQL \ 9.2 \ bin”时,Cygwin pip安装程序能够找到正确的PostgreSQL版本,并且能够使用pip成功安装该模块。(无论如何,这可能比使用Cygwin版本的PostgreSQL更可取,因为本机版本运行得更快)。

I’ve been battling with this for days, and have finally figured out how to get the “pip install psycopg2” command to run in a virtualenv in Windows (running Cygwin).

I was hitting the “pg_config executable not found.” error, but I had already downloaded and installed postgres in Windows. It installed in Cygwin as well; running “which pg_config” in Cygwin gave “/usr/bin/pg_config”, and running “pg_config” gave sane output — however the version installed with Cygwin is:

VERSION = PostgreSQL 8.2.11

This won’t work with the current version of psycopg2, which appears to require at least 9.1. When I added “c:\Program Files\PostgreSQL\9.2\bin” to my Windows path, the Cygwin pip installer was able to find the correct version of PostgreSQL, and I was able to successfully install the module using pip. (This is probably preferable to using the Cygwin version of PostgreSQL anyway, as the native version will run much quicker).


回答 16

在Fedora 24上:对于Python 3.x

sudo dnf install postgresql-devel python3-devel

sudo dnf install redhat-rpm-config

激活您的虚拟环境:

pip install psycopg2

On Fedora 24: For Python 3.x

sudo dnf install postgresql-devel python3-devel

sudo dnf install redhat-rpm-config

Activate your Virtual Environment:

pip install psycopg2

回答 17

Psycopg2取决于Postgres库。在Ubuntu上,您可以使用:

apt-get install libpq-dev

然后:

pip install psycopg2

Psycopg2 Depends on Postgres Libraries. On Ubuntu You can use:

apt-get install libpq-dev

Then:

pip install psycopg2

回答 18

对于Windows较低的用户,他们不得不从下面的链接安装psycopg2,只需将其安装到您设置的任何Python安装中即可。它将名为“ psycopg2”的文件夹放置在python安装的site-packages文件夹中。

之后,只需将该文件夹复制到virtualenv的site-packages目录中,就不会有问题。

这是您可以找到安装psycopg2的可执行文件的链接

http://www.lfd.uci.edu/~gohlke/pythonlibs/

For lowly Windows users were stuck having to install psycopg2 from the link below, just install it to whatever Python installation you have setup. It will place the folder named “psycopg2” in the site-packages folder of your python installation.

After that, just copy that folder to the site-packages directory of your virtualenv and you will have no problems.

here is the link you can find the executable to install psycopg2

http://www.lfd.uci.edu/~gohlke/pythonlibs/


回答 19

在OpenSUSE 13.2上,此操作已修复:

sudo zypper in postgresql-devel 

On OpenSUSE 13.2, this fixed it:

sudo zypper in postgresql-devel 

回答 20

我可以将其安装在Windows计算机上,并通过以下命令将Anaconda / Spyder与python 2.7结合使用:

 !pip install psycopg2

然后建立与数据库的连接:

 import psycopg2
 conn = psycopg2.connect(dbname='dbname',host='host_name',port='port_number', user='user_name', password='password')

I could install it in a windows machine and using Anaconda/Spyder with python 2.7 through the following commands:

 !pip install psycopg2

Then to establish the connection to the database:

 import psycopg2
 conn = psycopg2.connect(dbname='dbname',host='host_name',port='port_number', user='user_name', password='password')

回答 21

Arch基本发行版中:

sudo pacman -S python-psycopg2
pip2 install psycopg2  # Use pip or pip3 to python3

In Arch base distributions:

sudo pacman -S python-psycopg2
pip2 install psycopg2  # Use pip or pip3 to python3

回答 22

如果pip不起作用,则可以从此处https://pypi.python.org/pypi/psycopg2下载.whl文件 。python setup.py install

if pip is not working than you can download .whl file from here https://pypi.python.org/pypi/psycopg2 extract it.. than python setup.py install


回答 23

在Ubuntu上,我只需要postgres dev软件包:

sudo apt-get install postgresql-server-dev-all

*在virtualenv中测试

On Ubuntu I just needed the postgres dev package:

sudo apt-get install postgresql-server-dev-all

*Tested in a virtualenv


回答 24

在OSX 10.11.6(El Capitan)上

brew install postgresql
PATH=$PATH:/Library/PostgreSQL/9.4/bin pip install psycopg2

On OSX 10.11.6 (El Capitan)

brew install postgresql
PATH=$PATH:/Library/PostgreSQL/9.4/bin pip install psycopg2

回答 25

在具有Macports的OSX上:

sudo port install postgresql96
export PATH=/opt/local/lib/postgresql96/bin:$PATH

On OSX with macports:

sudo port install postgresql96
export PATH=/opt/local/lib/postgresql96/bin:$PATH

回答 26

我遇到了这个问题,主要原因是安装了2个相同的版本。一种通过postgres.app,另一种通过HomeBrew。

如果您选择仅保留APP:

brew unlink postgresql
pip3 install psycopg2

I was having this problem, the main reason was with 2 equal versions installed. One by postgres.app and one by HomeBrew.

If you choose to keep only the APP:

brew unlink postgresql
pip3 install psycopg2

回答 27

在macOS Mojave上,请确保您使用适用于我的Command Line Tools 10.3进行了最新更新-通过Software Update对其进行了更新,Mojave上的Command Line Tools的先前版本对我不起作用。

On macOS Mojave make sure you on newest update with Command Line Tools 10.3 – that worked for me – updated it with Software Update, previous version of Command Line Tools on Mojave did not work for me.


回答 28

试试这个Gentoo

emerge dev-libs/libpqxx

Try this in Gentoo:

emerge dev-libs/libpqxx

回答 29

在Windows上是这样工作
的在虚拟环境中通过pip安装flask之后,在命令提示符下运行此命令

>pip install psycopg2

检查一下

On windows this is how it works
On the command prompt after installing flask via pip in virtual environment, run this command

>pip install psycopg2

Check this


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。