postgresql 如何在 Windows 7 上安装 psycopg2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31934051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:02:50  来源:igfitidea点击:

How to install psycopg2 on windows 7

postgresqlpython-2.7psycopg2

提问by Rahul Shrivastava

I have tried pip install psycopg2and following errors i got:

我已经尝试过pip install psycopg2并出现以下错误:

Error:

错误:

(ScrapeProj) C:\Users\e2sn7cy\Documents\GitHub\scraper>pip install psycopg2==2.5.4
Collecting psycopg2==2.5.4
  Using cached psycopg2-2.5.4.tar.gz
    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 in c:\users\e2sn7cy\appdata\local\temp\pi
p-build-joij3x\psycopg2

Then I tried using easy_installby downloading the .exefile from Stickpoleand I got following errors :

然后我尝试easy_install通过.exeStickpole下载文件来使用,但出现以下错误:

(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj\Scripts>easy_install psycopg2-2.6.1.win-amd64-py2.7-pg9
.4.4-release.exe
Processing psycopg2-2.6.1.win-amd64-py2.7-pg9.4.4-release.exe
psycopg2.tests.test_module: module references __file__
psycopg2.tests.test_types_basic: module references __file__
creating 'c:\users\e2sn7cy\appdata\local\temp\easy_install-sa7asq\psycopg2-2.6.1-py2.7-win32.egg' and
 adding 'c:\users\e2sn7cy\appdata\local\temp\easy_install-sa7asq\psycopg2-2.6.1-py2.7-win32.egg.tmp'
to it
creating c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Extracting psycopg2-2.6.1-py2.7-win32.egg to c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages
Adding psycopg2 2.6.1 to easy-install.pth file

Installed c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Processing dependencies for psycopg2==2.6.1
Finished processing dependencies for psycopg2==2.6.1

But when I again tried to install through pip I am getting a new message that its already there :

但是当我再次尝试通过 pip 安装时,我收到一条新消息,它已经存在:

(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj>pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in c:\users\e2sn7cy\envs\scrapepro
j\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg

Then I thought ok I should try to import and then I got this error:

然后我想好我应该尝试导入,然后我收到了这个错误:

(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj>python
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\e2sn7cy\Envs\ScrapeProj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg\psycopg2\__
init__.py", line 50, in <module>
    from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: DLL load failed: %1 is not a valid Win32 application.

Now I am not able to understand what to do. Thanks

现在我无法理解该怎么做。谢谢

Edit 1: I have also added my PostgreSQL path to system path variables

编辑 1:我还将我的 PostgreSQL 路径添加到系统路径变量

path:C:\Program Files\PostgreSQL\9.4

path:C:\Program Files\PostgreSQL\9.4

path:C:\Program Files\PostgreSQL\9.4\bin

path:C:\Program Files\PostgreSQL\9.4\bin

回答by Craig Ringer

Download the pre-packaged binary release of psycopg2 for Windowsrather than compiling it yourself. It's a pain to compile on Windows. This is explained in the install documentation for psycopg2.

下载适用于 Windows 的 psycopg2预打包二进制版本,而不是自己编译。在 Windows 上编译很痛苦。这在 psycopg2 的安装文档中进行了解释

You might need to remove your half-installed attempts.

您可能需要删除安装了一半的尝试。

回答by Rahul Shrivastava

The correct answer was given by Craig Ringer, I am just adding this because I came to know that we should first check which version of 64 bit or 32bit Python installed on our system. If you have 32 bit version of Python and if you will try to install 64bit, then you will get the above error on importing. For checking the version:

正确答案由 Craig Ringer 给出,我只是添加这个,因为我知道我们应该首先检查我们的系统上安装了哪个版本的 64 位或 32 位 Python。如果您有 32 位版本的 Python 并且您将尝试安装 64 位,那么您将在导入时遇到上述错误。检查版本:

import platform

import platform

platform.architecture()

platform.architecture()

and you will get an output like this:

你会得到这样的输出:

('32bit','WindowsPE')

('32bit','WindowsPE')

回答by Adam Starrh

  1. Choose the appropriate version from this page:

    http://www.stickpeople.com/projects/python/win-psycopg/

  2. Right click and select copy link address

  3. Back at home, use easy_install <<Paste URL Here>>

  4. Freeze your new requirements: pip freeze > requirements.txt

  1. 从此页面选择合适的版本:

    http://www.stickpeople.com/projects/python/win-psycopg/

  2. 右键单击并选择 copy link address

  3. 回到家,使用 easy_install <<Paste URL Here>>

  4. 冻结您的新需求: pip freeze > requirements.txt

回答by evan_b

Now that psycopg is in PyPI (as of March 2017 it appears), just fire up a command prompt with administrator privileges and install like so:

现在 psycopg 在 PyPI 中(截至 2017 年 3 月它出现),只需使用管理员权限启动命令提示符并像这样安装:

python -m pip install -U pip
python -m pip install psycopg2

If you don't have pip installed, follow the instructions here to do that first: https://stackoverflow.com/a/12476379/2540707

如果您没有安装 pip,请先按照此处的说明进行操作:https: //stackoverflow.com/a/12476379/2540707

回答by Zakarías Frieriksson

pip install psycopg2-binary

pip 安装 psycopg2-binary

This worked for me

这对我有用

回答by bruinspaw

Step 1: Compilers Installation and configuration

第 1 步:编译器安装和配置

https://wiki.python.org/moin/WindowsCompilers

https://wiki.python.org/moin/WindowsCompilers

Before do anything, install or upgrade the Setuptools Python package. It contain compatibility improvements and add automatic use of compilers:

在做任何事情之前,安装或升级 Setuptools Python 包。它包含兼容性改进并添加编译器的自动使用:

pip install --upgrade setuptools

pip install --upgrade 安装工具

Step 2: Download and install PostgreSQL

第 2 步:下载并安装 PostgreSQL

Before you continue to install python packages inside you virtualenvs download postgres itself. It contains files that are needed when compiling the psycopg2 python package. Just use the PostgreSQL installer(version 10 for example).

在你继续安装 python 包之前,virtualenvs 下载 postgres 本身。它包含编译 psycopg2 python 包时需要的文件。只需使用 PostgreSQL 安装程序(例如版本 10)。

Important: add the postgres C:\Program Files\PostgreSQL\10\bin folder to your path. It contains the .dll needed for psycopg2.

重要提示:将 postgres C:\Program Files\PostgreSQL\10\bin 文件夹添加到您的路径中。它包含 psycopg2 所需的 .dll。

Step 3: Install psycopg2

第 3 步:安装 psycopg2

pip install psycopg2

pip 安装 psycopg2

Tested on Windows 7 + python 3.7

在 Windows 7 + python 3.7 上测试

回答by user1771806

The best/easiest way of installing packages (including this problematic psycopg2 ) for python on windows platform is to:

在 Windows 平台上为 python 安装包(包括这个有问题的 psycopg2 )的最好/最简单的方法是:

1) install anaconda and ... 2) use Anaconda Navigator GUI to install/update packages.

1) 安装 anaconda 和... 2) 使用 Anaconda Navigator GUI 来安装/更新软件包。

By this one avoids many inconsistencies and conflicts that occurs among different packages versions and installations.

通过这种方式避免了在不同软件包版本和安装之间发生的许多不一致和冲突。