简体   繁体   English

如何为 Python 3.x 安装 psycopg2?

[英]How do I install psycopg2 for Python 3.x?

Just started Python a few days ago and I'm using PyCharm to develop a web application with Django. I have libpq-dev python-dev packages already installed, but it's still throwing me the same error:几天前刚开始使用 Python,我正在使用 PyCharm 开发一个带有 Django 的 web 应用程序。我已经安装了 libpq-dev python-dev 包,但它仍然抛出同样的错误:

./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory

which according to Google is the issue that occurs when python-dev package isn't installed.根据 Google 的说法,这是未安装 python-dev package 时发生的问题。 Note that I'm running the install from within the PyCharm interface for a virtualenv that I created for 3.2 and 3.3 (not sure how to run it from the terminal).请注意,我正在从 PyCharm 界面中为我为 3.2 和 3.3 创建的 virtualenv 运行安装(不确定如何从终端运行它)。 Installing outside of the virtualenv still throws the same error, even when I install it from the terminal with setup.py.在 virtualenv 之外安装仍然会抛出相同的错误,即使我使用 setup.py 从终端安装它也是如此。 If I run pip install psycopg2 from the terminal, it succeeds, but it installs for Python 2.7.如果我从终端运行 pip install psycopg2,它会成功,但它会安装 Python 2.7。 According to their website, they have support for up to Python 3.2.根据他们的网站,他们最多支持 Python 3.2。

在Ubuntu上你运行这个:

sudo apt-get install python3-psycopg2

Just run this using the terminal: 只需使用终端运行:

$ sudo apt-get install python3-dev

This way, you could use gcc to build the module you're trying to use. 这样,您可以使用gcc构建您尝试使用的模块。

Another option that seems to provide a newer version of psycopg2 than the one in the python3-psycopg2 package (at least when I wrote this): 另一种选择似乎提供了比python3-psycopg2包中更新版本的psycopg2(至少在我写这篇文章时):

sudo apt-get install pip3
sudo apt-get install libpq-dev
sudo pip3 install psycopg2

For most operating systems, the quickest way to install Psycopg is using the wheel package available on PyPI:对于大多数操作系统,安装Psycopg的最快方法是使用 PyPI 上可用的 wheel package:

$ pip install psycopg2-binary

Check:查看:

$ pip freeze | grep -i psycopg2
psycopg2-binary==2.9.3

This will install a pre-compiled binary version of the module which does not require the build or runtime prerequisites.这将安装不需要构建或运行时先决条件的模块的pre-compiled二进制版本。

Or:或者:

$ sudo apt-get install libpq-dev
$ pip install psycopg2

Check:查看:

$ pip freeze | grep -i psycopg2
psycopg2==2.9.3

More info about psycopg vs psycopg-binary .有关psycopgpsycopg-binary更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM