使用Flask/Postgres安装psycopg2

1 投票
2 回答
2566 浏览
提问于 2025-04-18 00:32

我昨晚一直在寻找解决办法,但在尝试在Flask应用中安装psycopg2时,遇到了一个不常见的错误。我运行了这个命令:

pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz

结果出现了这个错误:

正在创建 build/temp.macosx-10.9-intel-2.7/psycopg

cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch x86_64 -arch i386 -pipe -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.4 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x090104 -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I. -I/Applications/Postgres.app/Contents/MacOS/include -I/Applications/Postgres.app/Contents/MacOS/include/server -c psycopg/psycopgmodule.c -o build/temp.macosx-10.9-intel-2.7/psycopg/psycopgmodule.o

clang: 错误: 未知参数: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

clang: 注意: 这将来会成为一个严重错误(无法降级为警告)

错误: 命令 'cc' 失败,退出状态为 1

---------------------------------------- 命令 /Users/jasdeep1/Dropbox/workspace/Printbase/venv/bin/python -c "import setuptools;file='/var/folders/9q/bg9_hgr16s7gt7gdbg8x79wr0000gn/T/pip-TbS4xF-build/setup.py';exec(compile(open(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record /var/folders/9q/bg9_hgr16s7gt7gdbg8x79wr0000gn/T/pip-Yavgs4-record/install-record.txt --single-version-externally-managed --install-headers /Users/jasdeep1/Dropbox/workspace/Printbase/venv/include/site/python2.7 失败,错误代码为 1,在 /var/folders/9q/bg9_hgr16s7gt7gdbg8x79wr0000gn/T/pip-TbS4xF-build 完整日志存储在 /Users/jasdeep1/.pip/pip.log

我不太确定该如何解决这个问题。我看到的答案似乎都没有解决这个问题。任何能让我朝正确方向前进的帮助都非常感谢。

如果需要,我很乐意分享更多调试输出,但不确定多少细节算太多。

2 个回答

0

安装步骤:打开终端(可以是编辑器或者本地的终端)

$ brew install postgresql
$ brew services start postgresql

然后在终端里进入SQL提示符,来创建一个数据库(不要离开终端):

$ psql postgres

创建数据库 ..

创建用户

授权 ...

连接示例:

def con_db():
    conn = None
    cursor = None

    try:
        conn = psycopg2.connect("host= dbname= user= password=")

        cursor = conn.cursor()
        sql_query = """select .."""
        cursor.execute(sql_query)

        # conn.commit()
        result = cursor.fetchall()  # fetchone()

    except Exception as e:
        print(e)
    finally:
        cursor.close()
        conn.close()
2

你可能想直接通过pip来安装psycopg2,不太明白你为什么要手动安装压缩包(不过,如果你已经安装了libpq-devbuild-essential,那手动安装应该也是可以的)。

$ pip install -U psycopg2

撰写回答