[python] Error: No module named psycopg2.extensions

I am trying to set up a PostgreSQL database for my django project, which I believe I have done now thanks to the replies to my last question Problems setting up a postgreSQL database for a django project. I am now trying to run the command 'python manage.py runserver' in Terminal to get my localhost up but when I run the command, I see this response...

Error: No module named psycopg2.extensions

I'm not sure what this means - I have tried to download psycopg2 but can't seem to find a way to download psycopg2 using homebrew. I have tried easy_install, pip install and sudo but all return errors like this...

Downloading http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.5.tar.gz
Processing psycopg2-2.4.5.tar.gz
Writing /tmp/easy_install-l7Qi62/psycopg2-2.4.5/setup.cfg
Running psycopg2-2.4.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-l7Qi62/psycopg2-2.4.5/egg-dist-tmp-PBP5Ds
no previously-included directories found matching 'doc/src/_build'
unable to execute gcc-4.0: No such file or directory
error: Setup script exited with error: command 'gcc-4.0' failed with exit status 1

How to fix this?

This question is related to python django postgresql psycopg2

The answer is


This one worked for me

python manage.py migrate


For Django 2 and python 3 install psycopg2 using pip3 :

pip3 install psycopg2

first install apt-get install python-setuptools

then try easy_install psycopg2


I encountered the No module named psycopg2.extensions error when trying to run pip2 install psycopg2 on a Mac running Mavericks (10.9). I don't think my stack trace included a message about gcc, and it also included a hint:

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

I looked for the pg_config file in my Postgres install and added the folder containing it to my path: /Applications/Postgres.app/Contents/Versions/9.4/bin. Your path may be different, especially if you have a different version of Postgres installed - I would just poke around until you find the bin/ folder. After doing this, the installation worked.


It seems that you need gcc-4.0, and it would be helpful to specify your OS type and version.

Maybe this question will help you a bit: Installing GCC to Mac OS X Leopard without installing Xcode

Update

I'm a Windows user, so I can't test your setup, but a quick google pointed to some more links:


pip3 install django-psycopg2-extension

I know i am late and there's lot of answers up here which also solves the problem. But today i also faced this problem and none of this helps me. Then i found the above magical command which solves my problem :-P . so i am posting this as it might be case for you too.
Happy coding.


This error raise because you not install postgres database in you project virtutal environment. you should run one of these command. from a terminal you can you command for sudo.

sudo apt-get install build-dep python-psycopg2

for pip (pip basically work for python)

pip install psycopg2

or

pip3 install psycopg2-binary

i'm pretty sure it will work for you.


Check if you have installed psycopg2 if not

sudo apt-get install psycopg2

Install the dependencies.

sudo apt-get build-dep python-psycopg2

These two commands should solve the problem.


you can install gcc for macos from https://github.com/kennethreitz/osx-gcc-installer
after instalation of gcc you'll be able to install psycopg with easy_install or with pip


pip install psycopg2-binary

The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: http://initd.org/psycopg/docs/install.html#binary-install-from-pypi.


In python 3.4, while in a virtual environment, make sure you have the build dependencies first:

sudo apt-get build-dep python3-psycopg2

Then install it:

pip install psycopg2 

On Alpine Linux (majority of the docker containers) do:

apk add postgresql-dev

Then:

pip install psycopg2-binary

I had such problem when trying to run python script as a sudo, while psycopg2 was installed via pip3 to my own user's directory.

I managed to resolve the issue for myself removing pip3 version, and just installing it via apt:

pip3 uninstall psycopg2
sudo apt install python3-psycopg2

The first thing to do is to install the dependencies.

sudo apt-get build-dep python-psycopg2

After that go inside your virtualenv and use

pip install psycopg2-binary

These two commands should solve the problem.


I used the extension after only importing psycopg2:

import psycopg2

...

psycopg2.extensions.AsIs(anap[i])

try this:
sudo pip install -i https://testpypi.python.org/pypi psycopg2==2.7b2
.. this is especially helpful if you're running into egg error

on aws ec2 instances if you run into gcc error; try this
1. sudo yum install gcc python-setuptools python-devel postgresql-devel
2. sudo su -
3. sudo pip install psycopg2


I installed it successfully using these commands:

sudo apt-get install libpq-dev python-dev
pip install psycopg2

This is what helped me on Ubuntu if your python installed from Ubuntu installer. I did this after unsuccessfully trying 'apt-get install' and 'pip install':

In terminal:

sudo synaptic

then in synaptic searchfield write

psycopg2

choose

python-psycopg2

mark it for installation using mouse right-click and push 'apply'. Of course, if you don't have installed synaptic, then first do:

sudo apt-get install synaptic

For macOS Mojave just run pip install psycopg2-binary. Works fine for me, python version -> Python 3.7.2


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to django

How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip Pylint "unresolved import" error in Visual Studio Code Is it better to use path() or url() in urls.py for django 2.0? Unable to import path from django.urls Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?' ImportError: Couldn't import Django Django - Reverse for '' not found. '' is not a valid view function or pattern name Class has no objects member Getting TypeError: __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries How to switch Python versions in Terminal?

Examples related to postgresql

Subtracting 1 day from a timestamp date pgadmin4 : postgresql application server could not be contacted. Psql could not connect to server: No such file or directory, 5432 error? How to persist data in a dockerized postgres database using volumes input file appears to be a text format dump. Please use psql Postgres: check if array field contains value? Add timestamp column with default NOW() for new rows only Can't connect to Postgresql on port 5432 How to insert current datetime in postgresql insert query Connecting to Postgresql in a docker container from outside

Examples related to psycopg2

Pandas read_sql with parameters Error: No module named psycopg2.extensions pg_config executable not found psycopg2: insert multiple rows with one query How to install psycopg2 with "pip" on Python? How to set up a PostgreSQL database in Django DatabaseError: current transaction is aborted, commands ignored until end of transaction block?