ImportError: No module named psycopg2 after install
Question:
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)
Answers:
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
I had the same problem on Ubuntu, and I tried apt-get
and pip
to no avail. I ended up uninstalling from both pip and apt-get, and then downloading the source package from here. Once you have the source, you can just run:
python setup.py build
sudo python setup.py install
I didn’t have to do anything else after that. Psycopg2 was available as an import within the default installation of python.
It’s odd, because the installation instructions specifically recommend using a package if one exists for your OS, but the source method is the only one that worked for me on Ubuntu.
Solution: install the package via yum or apt not pip.
sudo yum install python-psycopg2
and then psycopg2 can be used by all users.
pip install psycogp2
will fail with permission issue:
IOError: [Errno 13] Permission denied: '/usr/lib64/python2.7/site-packages/psycopg2-2.7.6.1.dist-info/METADATA'
I was experiencing problems trying to install this package. Could not install it at all.
https://pypi.org/project/psycopg2/
From that page I’ve found this option:
pip install psycopg2-binary
and boom! it worked!
Just use this instead:
sudo pip install psycopg2-binary==2.8.4
I ran into this issue when I copied the contents of one folder containing a virtual environment to a new one.
Even after running pip install psycopg2
with the venv active from the cli or from with VS Code,
My solution was to delete the virtual environment folder, create a new one and use pip to re-install all of the packages.
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)
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
I had the same problem on Ubuntu, and I tried apt-get
and pip
to no avail. I ended up uninstalling from both pip and apt-get, and then downloading the source package from here. Once you have the source, you can just run:
python setup.py build
sudo python setup.py install
I didn’t have to do anything else after that. Psycopg2 was available as an import within the default installation of python.
It’s odd, because the installation instructions specifically recommend using a package if one exists for your OS, but the source method is the only one that worked for me on Ubuntu.
Solution: install the package via yum or apt not pip.
sudo yum install python-psycopg2
and then psycopg2 can be used by all users.
pip install psycogp2
will fail with permission issue:
IOError: [Errno 13] Permission denied: '/usr/lib64/python2.7/site-packages/psycopg2-2.7.6.1.dist-info/METADATA'
I was experiencing problems trying to install this package. Could not install it at all.
https://pypi.org/project/psycopg2/
From that page I’ve found this option:
pip install psycopg2-binary
and boom! it worked!
Just use this instead:
sudo pip install psycopg2-binary==2.8.4
I ran into this issue when I copied the contents of one folder containing a virtual environment to a new one.
Even after running pip install psycopg2
with the venv active from the cli or from with VS Code,
My solution was to delete the virtual environment folder, create a new one and use pip to re-install all of the packages.