我正在使用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 ...

当前回答

在macOS Mojave上,请确保您在最新更新的命令行工具10.3 -这对我有用-用软件更新更新了它,Mojave上的命令行工具的以前版本对我不起作用。

其他回答

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包,因为过去它们在多线程环境中被证明是不可靠的。这可能已经在最近的版本中修复,但我从来没有设法重现失败。

Psycopg2依赖于Postgres库。 在Ubuntu上你可以使用:

apt-get install libpq-dev

然后:

pip install psycopg2

在windows上,这就是它的工作原理 在虚拟环境中通过pip安装flask后,在命令提示符上运行此命令

>pip install psycopg2

检查这个

我只在Python2中遇到过这个错误。我立即将虚拟环境升级到Python3.8,错误就不再存在了。

我可以在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')