python – ImportError:安装后没有名为psycopg2的模块

我已经在我的ubuntu服务器上的virtualenv上成功运行了sudo pip install psycopg2.

这是我正在尝试运行的代码:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://USERNAME:PASSWORD@localhost/mydb"
db = SQLAlchemy(app)
app.debug = True

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(100))

@app.route('/users/', methods = ['GET'])
def users():
    query = "SELECT id, name FROM users"

    results = User.query.from_statement(query).all()

    json_results = []
    for result in results:
        d = {'id' : result.id,
            'name' : result.name}
        json_results.append(d)

    res = jsonify(items=json_results)
    res.headers['Access-Control-Allow-Origin'] = '*'
    return res

但是,当用这个文件运行uWSGI时:uwsgi –socket 127.0.0.1:8080 –wsgi-file my_app.py –callable app –processes 4 –threads 2 –stats 127.0.0.1:9191,和使用SERVER_URL / users /调用它,我收到以下错误:

Traceback (most recent call last):
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "my_app.py", line 18, in users
    results = User.query.from_statement(query).all()
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 428, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 70, in __call__
    return self.registry()
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 903, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 139, in __init__
    bind=db.engine,
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 780, in engine
    return self.get_engine(self.get_app())
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 797, in get_engine
    return connector.get_engine()
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 473, in get_engine
    self._engine = rv = sqlalchemy.create_engine(info, **options)
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 344, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 73, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/home/ubuntu/my_app/my_app_venv/local/lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 401, in dbapi
    import psycopg2
ImportError: No module named psycopg2
[pid: 10902|app: 0|req: 3/3] 74.108.216.27 () {40 vars in 708 bytes} [Sun May 18 01:46:00 2014] GET /users/ => generated 0 bytes in 13 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

最佳答案 问题是您以超级用户身份安装了psycopg2,即使用“sudo”.当您以超级用户身份运行命令时,该命令将在具有超级用户环境的不同shell中执行,因此该程序包将安装在文件系统中可能无法访问“正常”环境的其他位置.您需要以普通用户的身份再次安装软件包,即没有前导’sudo’才能使用该软件包.简而言之,只需运行:

pip install psycopg2
点赞