为什么PostgreSQL不能在python中运行?

Tam*_*lik -2 python django postgresql

root@user:~# pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in /usr/local/lib/python2.7/dist-packages
Run Code Online (Sandbox Code Playgroud)

为什么PostgreSQL不能在python中运行?

root@user:~$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:29) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import postgresql
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named postgresql
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 5

调用该模块psycopg2,导入它而不是postgresql:

import psycopg2
Run Code Online (Sandbox Code Playgroud)

演示:

>>> import psycopg2
>>> psycopg2.__version__
'2.5.4 (dt dec pq3 ext)'
Run Code Online (Sandbox Code Playgroud)

虽然有PostgreSQL纯python python3特定的驱动程序调用py-postgresql.你可能正在混淆/引用它:

$ python3
Python 3.4.0 (default, Apr  3 2014, 20:36:56) 
>>> import postgresql
>>> postgresql.version
'1.1.0'
Run Code Online (Sandbox Code Playgroud)