ImportError: No module named psycopg2 after install
flask, python, uwsgi
Solution
The problem is that you installed psycopg2 as a superuser, i.e. using 'sudo'. When you run commands as a super user, the command gets executed in a different shell that has the superuser's environment, and as a result the package will be installed in a different location in the filesystem that may not be accessible to your 'normal' environment. You need to install the package again as a regular user, i.e. without the leading 'sudo' in order to use the package. In short, just run:
pip install psycopg2
Problem
I've run `sudo pip install psycopg2` successfully from my `virtualenv` on my ubuntu server. This is the code I'm trying to run: ``` 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 ``` however, when running uWSGI with this file like this :`uwsgi --socket 127.0.0.1:8080 --wsgi-file my_app.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191`, and call on it with `SERVER_URL/users/`, I get the following error: ``` 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) ```