107

我正在尝试为我的 django 项目设置一个 PostgreSQL 数据库,我相信我现在已经完成了,这要归功于对我最后一个问题Problems setting up a postgreSQL database for a django project的回复。我现在正在尝试在终端中运行命令“python manage.py runserver”来启动我的本地主机,但是当我运行命令时,我看到了这个响应......

Error: No module named psycopg2.extensions

我不确定这意味着什么——我尝试下载 psycopg2,但似乎找不到使用自制软件下载 psycopg2 的方法。我尝试过 easy_install、pip install 和 sudo,但都返回这样的错误......

Downloading http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.5.tar.gz
Processing psycopg2-2.4.5.tar.gz
Writing /tmp/easy_install-l7Qi62/psycopg2-2.4.5/setup.cfg
Running psycopg2-2.4.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-l7Qi62/psycopg2-2.4.5/egg-dist-tmp-PBP5Ds
no previously-included directories found matching 'doc/src/_build'
unable to execute gcc-4.0: No such file or directory
error: Setup script exited with error: command 'gcc-4.0' failed with exit status 1

如何解决这个问题?

4

20 回答 20

190

首先要做的是安装依赖项。

sudo apt-get build-dep python-psycopg2
sudo apt install python3-psycopg2 # Python 3

之后进入你的 virtualenv 并使用:

pip install psycopg2-binary

这两个命令应该可以解决问题。

于 2013-01-13T22:28:38.447 回答
38
pip install psycopg2-binary

psycopg2 轮子包将从 2.8 版重命名;为了继续从二进制安装,请改用“pip install psycopg2-binary”。有关详细信息,请参阅:http: //initd.org/psycopg/docs/install.html#binary-install-from-pypi

于 2018-03-15T20:34:52.900 回答
9

使用Django 2python 3安装:psycopg2_pip3

pip3 install psycopg2
于 2018-01-04T15:34:07.913 回答
6

对于macOS Mojave刚刚运行pip install psycopg2-binary。对我来说很好,python版本->Python 3.7.2

于 2019-03-17T18:34:12.573 回答
5

我使用以下命令成功安装了它:

sudo apt-get install libpq-dev python-dev
pip install psycopg2
于 2016-06-09T05:05:14.167 回答
4

This is what helped me on Ubuntu if your python installed from Ubuntu installer. I did this after unsuccessfully trying 'apt-get install' and 'pip install':

In terminal:

sudo synaptic

then in synaptic searchfield write

psycopg2

choose

python-psycopg2

mark it for installation using mouse right-click and push 'apply'. Of course, if you don't have installed synaptic, then first do:

sudo apt-get install synaptic
于 2013-04-04T21:32:58.287 回答
4

首次安装apt-get install python-setuptools

然后尝试easy_install psycopg2

于 2012-10-04T14:31:20.897 回答
3

我最近遇到了同样的问题,这段代码有效。

sudo apt-get install libpq-dev python-dev-is-python3

然后

pip3 install psycopg2
于 2021-03-28T05:59:56.580 回答
2

在 python 3.4 中,在虚拟环境中,请确保首先具有构建依赖项:

sudo apt-get build-dep python3-psycopg2

然后安装它:

pip install psycopg2 
于 2016-06-12T22:15:32.867 回答
2
pip3 install django-psycopg2-extension

我知道我迟到了,这里有很多答案也解决了这个问题。但是今天我也遇到了这个问题,这些都对我没有帮助。然后我发现上面的神奇命令解决了我的问题:-P。所以我发布这个可能也是你的情况。
快乐编码。

于 2020-11-06T04:41:39.480 回答
2

在 Alpine Linux(大多数 docker 容器)上:

apk add postgresql-dev

然后:

pip install psycopg2-binary
于 2018-12-25T13:41:38.867 回答
1

我只在导入 psycopg2 后使用了扩展:

import psycopg2

...

psycopg2.extensions.AsIs(anap[i])
于 2012-10-04T13:18:36.730 回答
1

看来您需要gcc-4.0,并且指定您的操作系统类型和版本会有所帮助。

也许这个问题会对你有所帮助:Installing GCC to Mac OS X Leopard without installed Xcode

更新

我是 Windows 用户,所以我无法测试您的设置,但快速 google 指出了更多链接:

于 2012-10-04T13:33:47.660 回答
1

我在尝试将 python 脚本作为 sudo 运行时遇到了这样的问题,而 psycopg2 是通过 pip3 安装到我自己的用户目录中的。

我设法为自己解决了这个问题,删除了 pip3 版本,并通过 apt 安装它:

pip3 uninstall psycopg2
sudo apt install python3-psycopg2
于 2020-06-22T20:20:19.627 回答
1

try this:
sudo pip install -i https://testpypi.python.org/pypi psycopg2==2.7b2
.. this is especially helpful if you're running into egg error

on aws ec2 instances if you run into gcc error; try this
1. sudo yum install gcc python-setuptools python-devel postgresql-devel
2. sudo su -
3. sudo pip install psycopg2

于 2017-02-24T23:33:16.420 回答
1

No module named psycopg2.extensions尝试在pip2 install psycopg2运行 Mavericks (10.9) 的 Mac 上运行时遇到错误。我不认为我的堆栈跟踪包含有关 gcc 的消息,它还包含一个提示:

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

我在我的 Postgres 安装中查找了该pg_config文件,并将包含它的文件夹添加到我的路径中:/Applications/Postgres.app/Contents/Versions/9.4/bin. 您的路径可能不同,特别是如果您安装了不同版本的 Postgres - 我会四处寻找,直到您找到该bin/文件夹​​。完成此操作后,安装工作。

于 2016-06-15T13:41:55.797 回答
1

这个对我有用

python manage.py 迁移

于 2020-05-20T04:59:35.683 回答
0

您可以在安装 gcc 后从https://github.com/kennethreitz/osx-gcc-installer 为 macos 安装 gcc,您将能够使用或使用
psycopg 安装easy_installpip

于 2012-10-04T14:02:28.927 回答
0

如果没有,请检查您是否安装了 psycopg2

sudo apt-get install psycopg2

安装依赖项。

sudo apt-get build-dep python-psycopg2

这两个命令应该可以解决问题。

于 2013-03-12T06:25:36.943 回答
0

出现此错误是因为您没有在项目虚拟环境中安装 postgres 数据库。您应该运行这些命令之一。从终端您可以命令 sudo。

sudo apt-get install build-dep python-psycopg2

对于 pip(pip 基本上适用于 python)

pip install psycopg2

或者

pip3 install psycopg2-binary

我很确定它会为你工作。

于 2021-01-15T11:37:29.450 回答