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

时间:2014-05-18 01:54:55

标签: python flask uwsgi

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

这是我尝试运行的代码:

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)

3 个答案:

答案 0 :(得分:6)

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

pip install psycopg2

答案 1 :(得分:1)

解决方案:通过yum或apt pip来安装软件包。

sudo yum install python-psycopg2

然后所有用户都可以使用psycopg2。

pip install psycogp2

将因权限问题而失败:

IOError: [Errno 13] Permission denied: '/usr/lib64/python2.7/site-packages/psycopg2-2.7.6.1.dist-info/METADATA'

答案 2 :(得分:0)

我在Ubuntu上遇到了同样的问题,我尝试了apt-getpip无济于事。我最终从pip和apt-get卸载,然后从here下载源包。获得源代码后,您就可以运行:

python setup.py build
sudo python setup.py install

之后我没有做任何其他事情。 Psycopg2在默认的python安装中可以作为导入使用。

这很奇怪,因为安装说明特别建议使用一个包,如果你的操作系统存在一个包,但源方法是唯一一个在Ubuntu上工作的方法。