我正在使用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 ...
注意:自从一段时间以前,在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
sudo apt install libpq-dev python-dev
如果这还不够,那就试试
sudo apt install build-essential
or
sudo apt install postgresql-server-dev-all
在再次安装psycopg2之前。
CentOS 6
请看班杰的回答
操作系统
请看nichochar的回答
我已经为此奋斗了几天,终于弄清楚如何在Windows的virtualenv中运行“pip install psycopg2”命令(运行Cygwin)。
我碰到了“pg_config可执行文件未找到”错误,但我已经在Windows中下载并安装了postgres。它也安装在Cygwin;在Cygwin中运行"which pg_config"会给出"/usr/bin/pg_config",并且运行"pg_config"会给出正常的输出——然而安装在Cygwin中的版本是:
VERSION = PostgreSQL 8.2.11
这不适用于当前版本的psycopg2,该版本似乎至少需要9.1。当我将“c:\Program Files\PostgreSQL\9.2\bin”添加到我的Windows路径中时,Cygwin pip安装程序能够找到正确的PostgreSQL版本,并且我能够成功地使用pip安装模块。(这可能比使用Cygwin版本的PostgreSQL更好,因为原生版本会运行得更快)。
这对我来说是有效的(关于RHEL, CentOS:
sudo yum install postgresql postgresql-devel python-devel
现在在你的pip install中包含你的postgresql二进制目录的路径:
sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2
确保包含正确的路径。就这些。
更新:对于python3,请安装python3-devel而不是python-devel
到目前为止,答案太像魔法食谱了。您收到的错误告诉您pip无法找到PostgreSQL查询库中所需的部分。这可能是因为你把它安装在一个非标准的地方,这就是为什么消息建议使用——pg-config选项。
但更常见的原因是根本没有安装libpq。这通常发生在你没有安装PostgreSQL服务器的机器上,因为你只想运行客户端应用程序,而不是服务器本身。每个操作系统/发行版都是不同的,例如在Debian/Ubuntu上你需要安装libpq-dev。这允许你根据PostgreSQL查询库编译和链接代码。
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.
在安装psycopg2之前,您需要安装python-dev包。
如果你在Linux(也可能是其他系统,但我没有经验)上工作,在安装开发包时,你需要确保你运行的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。
MacOS系统安装
Following are the steps, which worked for me and my team members while installing psycopg2 on Mac OS Big Sur and which we have extensively tested for Big Sur. Before starting make sure you have the Xcode command-line tool installed. If not, then install it from the Apple Developer site. The below steps assume you have homebrew installed. If you have not installed homebrew then install it. Last but not the least, it also assumes you already have PostgreSQL installed in your system, if not then install it. Different people have different preferences but the default installation method on the official PostgreSQL site via Enterprise DB installer is the best method for the majority of people.
Put up the linkage to pg_config file in your .zshrc file by: export PATH="$PATH:/Library/PostgreSQL/12/bin:$PATH". This way you are having linkage with the pg_config file in the /Library/PostgreSQL/12/bin folder. So if your PostgreSQL installation is via other means, like Postgres.app or Postgres installation via homebrew, then you need to have in your .zshrc file the link to pg_config file from the bin folder of that PostgreSQL installation as psycopg2 relies on that.
Install OpenSSL via Homebrew using the command brew install openssl. The reason for this is that libpq, the library which is the basis of psycopg2, uses openssl - psycopg2 doesn't use it directly. After installing put the following commands in your .zshrc file:
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
By doing this you are creating necessary linkages in your directory. These commands are suggested by brew while you install openssl and have been directly picked up from there.
Now comes the most important step, which is to install libpq using the command brew install libpq. This installs libpq library. As per the documentation
libpq是C应用程序程序员到PostgreSQL的接口。libpq是一组库函数,允许客户端程序将查询传递给PostgreSQL后端服务器,并接收这些查询的结果。
使用brew Link libpq链接libpq,如果这不起作用,那么使用命令:brew Link libpq——force。
还要在.zshrc文件中放入以下export PATH="/usr/local/opt/libpq/bin:$PATH"。这将为libpq库创建所有必要的链接。
现在重新启动终端或使用以下命令源~/.zshrc。
即使在conda环境中工作,这也可以工作。
注意:pip install psycopg2-binary应该避免,因为根据psycopg2库的开发人员的说法
不鼓励在生产环境中使用-binary包,因为过去它们在多线程环境中被证明是不可靠的。这可能已经在最近的版本中修复,但我从来没有设法重现失败。