如何为 Python 3.x 安装 psycopg2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20863295/
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
How do I install psycopg2 for Python 3.x?
提问by Lunyx
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. 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). Installing outside of the virtualenv still throws the same error, even when I install it from the terminal with setup.py. If I run pip install psycopg2 from the terminal, it succeeds, but it installs for Python 2.7. According to their website, they have support for up to Python 3.2.
根据谷歌的说法,这是未安装 python-dev 包时发生的问题。请注意,我正在 PyCharm 界面中为我为 3.2 和 3.3 创建的 virtualenv 运行安装(不确定如何从终端运行它)。在 virtualenv 之外安装仍然会引发相同的错误,即使我使用 setup.py 从终端安装它。如果我从终端运行 pip install psycopg2,它会成功,但它会为 Python 2.7 安装。根据他们的网站,他们最多支持 Python 3.2。
采纳答案by Moayad Mardini
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 来构建您尝试使用的模块。
回答by Frontware
On Ubuntu you just run this:
在 Ubuntu 上,你只需运行这个:
sudo apt-get install python3-psycopg2
回答by sudo
Another option that seems to provide a newer version of psycopg2 than the one in the python3-psycopg2package (at least when I wrote this):
另一个选项似乎提供了比python3-psycopg2包中的版本更新的 psycopg2 版本(至少在我写这篇文章的时候):
sudo apt-get install pip3
sudo apt-get install libpq-dev
sudo pip3 install psycopg2

