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

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.

Who is Participating?
LunarNRGConnect With a Mentor Commented:
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.

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?
cshore12Author Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

cshore12Author Commented:
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?
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.

cshore12Author Commented:
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
cshore12Author Commented:
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.
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.
cshore12Author Commented:
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...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.