This post is a quick note to all about how to install psycopg2 in Linux. I used WSL 2 running Ubuntu 20.04.
Background
I was trying to set up a toy project to learn Django using PostgreSQL. When I tried to install the recommend psycopg2 package from pip, I received two error messages.
The first error I received was: error: invalid command 'bdist_wheel' . My second error was related to C / C++ and was in my log files:
In file included from psycopg/adapter_asis.c:28:
./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory
35 | #include <Python.h>
| ^~~~~~~~~~
compilation terminated.
The Steps
- I installed the following dependencies:
python-dev
,python3-dev
. - I installed wheels using
pip3 install wheel
. - I installed
python[your version]-dev
using Ubuntu'ssudo
command.
Explanation
The root cause of the problem was that psycopg2 was looking for headers for Python 3.8.5. Since I am using Python 3.9.5, the headers could not be located and psycopg2 could not install. After installing python3.9-dev
, psycopg2 successfully installed on my system.
I hope that this short post saves people time and energy.
Additional references:
SO - Invalid command bdist wheel on Travis CI
SO - How to fix error: invalid command 'bdist_wheel'
Top comments (1)
Thanks, that is very helpful.
Hopefully people with this error will find your solution.