2

I have installed postgres and Psycopg2 using apt-get on my linux.

I want to use postgres for one of my django project.

I have created virtualenv but i am not able to work with psycopg2 when i add it in db settings.

Here is setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'secondnginxapp',
        'USER': 'myprojectuser',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '',
    }
}

I run the server and got the error Error loading psycopg2 module: No module named 'psycopg2'

I check installed psycopg2 with following command.

 python -c "import psycopg2; print(psycopg2.__version__)"

Output: 2.4.5 (dt dec mx pq3 ext)

How to use psycopg2 with virtualenv? need help.

(I am learning python and django)

I tried to install in virtualenv as well.

pip install psycopg2 then error is Error: b'You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.\n'

Then i tried pip install libpq-dev now error is No matching distribution found for libpq-dev

11
  • Have you tried to use pip to install psycopg2 inside virtualenv? It is possible that you have 2 different versions of python and apt-get installed it just for python2. Commented Jun 19, 2016 at 6:44
  • How do you run the server? What command do you use? And is the virtualenv activated? Commented Jun 19, 2016 at 6:45
  • @Morishiri Yes i did. please check updated question. Commented Jun 19, 2016 at 6:50
  • @jpmc26 virtualenv is activated. I tried python manage.py runserver Commented Jun 19, 2016 at 6:50
  • @shri I think that you have your answer in updated question. Install postgresql-server-dev-X.Y (replace X.Y with version - 9.4 is the newest now afaik - using apt-get) Commented Jun 19, 2016 at 6:52

2 Answers 2

2

The problem here is that you have used apt-get to install the driver, which has installed it in your system python's package directory; and by default the virtual environment is created with no system packages.

So even though the command works when you are outside the virtual environment (using the system Python), it doesn't work inside the virtual environment.

You have two options to fix this:

  1. Delete the file no-global-site-packages.txt found in your virtual environment's Python installation.

    So if you created a virtual environment at /home/env/my-env/, then you would execute rm /home/env/my-env/lib/python3.4/no-global-site-packages.txt

    Once that file is removed, the virtual environment will look in the global environment for any missing packages.

  2. Install the system libraries that will allow you to build the extension. For ubuntu this is sudo apt install build-essential python-dev python3.4-dev libpq-dev. Once you run that command, you can then pip install psycopg2 in your virtual environment.

Sign up to request clarification or add additional context in comments.

2 Comments

with all respect sir, is it a actual solution?
Thank you very much for details.
1

The solution is to install server version of postgresql (It seems like you have only client installed). On Ubuntu:

sudo apt-get install postgresql-server-dev-X.Y

where the X.Y is version of the package.

Then, inside virtualenv environment install psycopg2 module:

pip install psycopg2

This should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.