如何在虚拟环境中安装psycops2?

时间:2020-06-07 14:32:44

标签: python django postgresql web web-applications

我需要安装psycopg2才能将PostgreSQL与django连接起来。我已经尝试过此命令。

pip install psycopg2

但是显示了此消息:

 Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source.  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'.

    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.

    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).


    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\Rafid\AppData\Local\Temp\pip-install-oknhhl43\psycopg2\

我是Web开发的新手,所以详细的解决方案将对您有所帮助...谢谢

2 个答案:

答案 0 :(得分:2)

安装预编译的二进制文件,然后安装psycopg2

pip install psycopg2-binary

答案 1 :(得分:0)

psycopg2的安装失败,因为它找不到运行的PostgreSQL可执行文件以查找本地PostgreSQL安装的配置详细信息。

有两种选择:

  1. 下载并安装PostgreSQL,确保它在PATH上,然后重新运行pip install psycopg2

    您可以从download page获取Windows的PostgreSQL安装程序。

  2. 改为运行pip install psycopg2-binary


请注意,Psycopg2 installation instructions声明PostgreSQL是先决条件。

但是,进一步解释说pg_config仅在构建时需要。