python - ImportError - 帮助配置环境以与 postgresql 模块一起使用
python - ImportError - help configure environment to use with postgresql module
Python 这里是新手。我正在尝试弄清楚如何使用 python.I 连接到 postgresql 数据库需要帮助来设置此环境(带有 Fedora 12 的来宾 VM)。我有 postgresql 9.3 并且想使用我认为 postgres 附带的 Psycopg2?
我收到以下消息:
$ python LearningPsycopg2.py
Traceback (most recent call last):
File "LearningPsycopg2.py", line 5, in <module>
import psycopg2
ImportError: No module named psycopg2
我安装了 postgres 并且 python 安装了。我不确定 python 是如何安装的,但这是我目前所拥有的:
-bash-4.3$ which python
/bin/python
-bash-4.3$ python -V
Python 2.7.8
-bash-4.3$ cd /bin/
-bash-4.3$ ls -ltr python*
-rwxr-xr-x. 1 root root 301 Jun 18 2014 python3-mako-render
-rwxr-xr-x. 1 root root 301 Aug 18 2014 python-coverage
lrwxrwxrwx. 1 root root 15 Aug 18 2014 python2-coverage -> python-coverage
lrwxrwxrwx. 1 root root 9 Nov 3 2014 python3 -> python3.4
-rwxr-xr-x. 2 root root 11288 Nov 3 2014 python3.4m
-rwxr-xr-x. 2 root root 11288 Nov 3 2014 python3.4
lrwxrwxrwx. 1 root root 9 Nov 10 2014 python2 -> python2.7
lrwxrwxrwx. 1 root root 7 Nov 10 2014 python -> python2
-rwxr-xr-x. 1 root root 7120 Nov 10 2014 python2.7
这是我的 python 脚本:
#!/bin/python2.7
#
# Small script to show PostgreSQL and Pyscopg together
#
import psycopg2
try:
conn = psycopg2.connect("dbname='foo_bar_99999' user='foo_bar' host='localhost' password='abc#123!'")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
try:
cur.execute("""SELECT * from foo_bar_99999.ups_date""")
except:
print "I can't SELECT from ups_date"
rows = cur.fetchall()
for row in rows:
print " ", row[1][1]
您必须实际安装 pyscopg2,默认情况下它不会随 Python 或 PostgreSQL 一起提供。有very detailed instructions here.
附带说明一下,更改您的 shebang 行以适应不同的位置可能是个好主意 Python 可以安装。
#!/usr/bin/env python2
这应该调用用户为他指定为 "the default" 的 python2 实例。
Python 这里是新手。我正在尝试弄清楚如何使用 python.I 连接到 postgresql 数据库需要帮助来设置此环境(带有 Fedora 12 的来宾 VM)。我有 postgresql 9.3 并且想使用我认为 postgres 附带的 Psycopg2?
我收到以下消息:
$ python LearningPsycopg2.py
Traceback (most recent call last):
File "LearningPsycopg2.py", line 5, in <module>
import psycopg2
ImportError: No module named psycopg2
我安装了 postgres 并且 python 安装了。我不确定 python 是如何安装的,但这是我目前所拥有的:
-bash-4.3$ which python
/bin/python
-bash-4.3$ python -V
Python 2.7.8
-bash-4.3$ cd /bin/
-bash-4.3$ ls -ltr python*
-rwxr-xr-x. 1 root root 301 Jun 18 2014 python3-mako-render
-rwxr-xr-x. 1 root root 301 Aug 18 2014 python-coverage
lrwxrwxrwx. 1 root root 15 Aug 18 2014 python2-coverage -> python-coverage
lrwxrwxrwx. 1 root root 9 Nov 3 2014 python3 -> python3.4
-rwxr-xr-x. 2 root root 11288 Nov 3 2014 python3.4m
-rwxr-xr-x. 2 root root 11288 Nov 3 2014 python3.4
lrwxrwxrwx. 1 root root 9 Nov 10 2014 python2 -> python2.7
lrwxrwxrwx. 1 root root 7 Nov 10 2014 python -> python2
-rwxr-xr-x. 1 root root 7120 Nov 10 2014 python2.7
这是我的 python 脚本:
#!/bin/python2.7
#
# Small script to show PostgreSQL and Pyscopg together
#
import psycopg2
try:
conn = psycopg2.connect("dbname='foo_bar_99999' user='foo_bar' host='localhost' password='abc#123!'")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
try:
cur.execute("""SELECT * from foo_bar_99999.ups_date""")
except:
print "I can't SELECT from ups_date"
rows = cur.fetchall()
for row in rows:
print " ", row[1][1]
您必须实际安装 pyscopg2,默认情况下它不会随 Python 或 PostgreSQL 一起提供。有very detailed instructions here.
附带说明一下,更改您的 shebang 行以适应不同的位置可能是个好主意 Python 可以安装。
#!/usr/bin/env python2
这应该调用用户为他指定为 "the default" 的 python2 实例。