Using mingw to expose object libraries to C compiler and eventually have psycopg2 work

Posted on 2010-11-22
Last Modified: 2012-05-10
I am trying to install the adapter psycopg2 for postgresql. (32-bit Win32 on an XP box).  Apparently there's a never ending loop of garbage that I have to get through in order to do this.  

1) I first create a site to hold a new django project
2) I then create a new project using ' startproject xxx".  This works fine.
3) I then downloaded and installed the psycopg2 database adapter, which initially seemed to go fine.
4) I then set up my setting file, with the following parameters:

[settings file]
    'default': {
        'ENGINE': 'postgresql_psycopg2', # Add 'django.db.backends.', 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'first',                      # Or path to database file if using sqlite3.
        'USER': 'Charlie Shore',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.

Please note that the database name and user are legitimate.

5) When I now go to the site directory on the command line, and type 'python shell', the output erred out with the following message:

Output:  File "C:\Python26\lib\site-packages\django\db\backends\postgresql_psycopg2\bas", line 24, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: DLL
load failed: The specified module could not be found.

6) Now I change the database name (in the setting file) to 'django.db.backends.', and am able to get the interactive manager.

7) Following on step 6, I'm able to connect interactively using 'from django.db import connection'.  But then when I try to define my cursor (cursor = connection.cursor()), I get the following:

Output: Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python26\lib\site-packages\django\db\backends\dummy\", line 15
, in complain
    raise ImproperlyConfigured("You haven't set the database ENGINE setting yet.
ImproperlyConfigured: You haven't set the database ENGINE setting yet.

8) As I understand it, (at least on most OS), I now need both the python-dev and libpg_dev modules for this to work right.  But maybe on Windows, I only need to expose the object libraries to the C compiler.  So I then installed the MinGW GNU development environment to the default location.

9) Using MinGW-get, I then installed the gcc, g++, binutils, and win32api components.  Although most things seem to install OK, I get a message 'mingw-get: *** ERROR *** package: unknown package' with each new package.

10) Finally I run the command shown on the next line, with the following result:

C:\Python26>gcc -I\python26\include -l\python26 -L\python26\libs

Output: c:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../libmingw32.a(main.o):main.c:(.tex
t+0xd2): undefined reference to `WinMain@16'
collect2: ld returned 1 exit status

At this point I'm so confused I don't know which way to go.  If I can just start by getting gcc to work, I may be able to work my way back up this tortuous chain.

Thanks in advance.

Question by:cshore12
  • 5
  • 5

Expert Comment

ID: 34190163
Let's back up a little. You say that the psycopg2 install seemed to go fine (in step 3) ...

Please post the output of the following commands?
c:\> python --version
c:\> python -c "import os; print os.__file__"
c:\> python -c "import psycopg2; print psycopg2.__version__"
c:\> python -c "import django; print django.get_version()"

Open in new window

What version of the psycopg2 installer are you using? These are usually tied to a specific python version. Did you use easy_install? Do you have more than one version of python installed?

Author Comment

ID: 34190601
Thanks for your rapid response:

1) python 2.6.6

2) C:\Python26\lib\os.pyc

3) Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python26\lib\site-packages\psycopg2\", line 69, in <module
    from _psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: DLL load failed: The specified module could not be found.

4) 1.2.3

I obviously misstated my case when I said that psycopg2 had installed directly.  I originally thought I had installed psycopg2-2.2.2, but then was led to believe that I had to expose the object libraries and header files to the C compiler for psycopg2 to be fully installed:

The concept of "python-dev package" is a Debian concept which doesn't
apply to Windows. The standard installation on Windows provides the
header and library files that you need for interfacing with C.

Don't put things where your C compiler can find them; do a standard
no-frills install of Python2.4 using the .msi installer and tell your C
compiler where the goodies are.

E.g. using the MinGW32 gcc, you'd need things like:

gcc -I\python24\include -lpython24 -L\python24\libs

Expert Comment

ID: 34190829
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!


Author Comment

ID: 34193047
No, I downloaded a tar file (specifically, psycopg2-2.2.2.tar).  From your question, I take it that Windows will only work with the binaries.  Should I get rid of my current psycopg files/folders and reinstall based on your recommended path?

Expert Comment

ID: 34196821
No, you can compile from source on windows. I've done it before, but not recently. However, if you don't have a good reason to do this, ie. you need to change some pre-compile time configuration option, then you're just punishing yourself. I'd go with the binary installer.

You should be able to just install over the top of what you have, but if you have the same, or a similar, issue after installing the binary package then get rid of the current psycopg files/folders and try again.


Author Comment

ID: 34209383
I downloaded the executable and entered the commands as before.  Although I used several different values for database name and user (In my setting file), I consistently got the attached error.  

The databases I tried to tap into were either owned by 'Charlie Shore' or postgres.  Therefore, it doesn't seem to matter what database name or user I use in the settings file.  Is there a way to parse the connection string to see what's going on?

Have a great Thanksgiving! dajngo-conn-string-error.txt

Author Comment

ID: 34226733
Eureka!  I created a third user and, after putting its name in the settings file, am now able to proceed with the project.  Apparently 'Charlie Shore' wasn't accepted either because it's two words, or possibly because I'm the database owner.  I can also see why user postgres was also not acceptable.

Expert Comment

ID: 34243970
Hmmm, interesting.

I think that might be a bug in psycopg2 -- or there is a special way to quote the whitespace that I've not seen before.

Honestly, I've never worked with a username that included spaces -- strange. I can reproduce your error by calling psycopg2.connect with "... user='test user' ... ", and several variations.

If I figure out the work-around, I'll post it here.

btw - sorry for the delayed response. I hope you had a great Thanksgiving as well.

Accepted Solution

LunarNRG earned 500 total points
ID: 34244068
Okay, sorry again. My bad.

Try this ... (wrap in double-quotes, leave single-quotes)

'USER': "'Charlie Shore'"

Open in new window

Whitespace in usernames should work fine, if quoted properly.


Author Closing Comment

ID: 34268875
LunarNPG -- Well, I've moved on.  I created a superuser with no white spaces, and that seems to work fine.  At some point I'll try your suggestion to single quote around the white spaces.  In the meantime, the points are yours...

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question