Thread: Using with Python 3, possible? how?

Using with Python 3, possible? how?

From
Leon Starr
Date:
Greetings all, newbie here.  I'm trying to use the latest release of psycopg2 with Python3 and having no success so
far.

I'm on Mac OS X Lion (Xcode installed) and I have both 2.7 and 3.2.2 Python installations with 3 set as my default
Python
and these are both macports installations.

So if I fire up my 2.7 IDLE session and do: import psycopg2
it appears to work okay, i.e. no errors.  (And yes, I am sure it's the 2.7 session)

But the same import in a 3.2.2 IDLE session tells me that no such module exists.
Well, duh, of course, because psycopg2 is probably installed in the 2.7 site package
directory.

My two part question is 1) can I use psycopg2 with Python3?  (The psycopg2 home site leads me to believe so)
if so 2) How?  Here's what I've tried so far:
    2a) easy_install would like to install in the 2.7 site install directory, okay, but it's already there, so no
thanks
        (should I try to force it into the 3.x site installation directory somehow?)
    2b) I tried having easy_install load psycopg2 into a separate, empty directory which I put into my PYTHONPATH
        variable, just to see what would happen.  Didn't work.  (See traceback below)
    2c) Should I somehow adjust my package search path in 3.2.2 to look in the 2.7 package directory?
        What could possibly go wrong with that?  ;)

So, should I be doing a, b, c or none of the above?  I eagerly await enlightenment and thank you for your time!

- Leon Starr

(from 3.2.2 IDLE session)
>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/starr/SDEV/Python/Site_P3/psycopg2-2.4.5-py2.7-macosx-10.7-x86_64.egg/psycopg2/__init__.py", line 67, in
<module>
    from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Users/starr/SDEV/Python/Site_P3/psycopg2-2.4.5-py2.7-macosx-10.7-x86_64.egg/psycopg2/_psycopg.so,
2):Symbol not found: _PyBuffer_Type 
  Referenced from: /Users/starr/SDEV/Python/Site_P3/psycopg2-2.4.5-py2.7-macosx-10.7-x86_64.egg/psycopg2/_psycopg.so
  Expected in: flat namespace
 in /Users/starr/SDEV/Python/Site_P3/psycopg2-2.4.5-py2.7-macosx-10.7-x86_64.egg/psycopg2/_psycopg.so

(It's been a few years since I've written any Python, so I'm a bit groggy on the package installation
procedures, but it's all slowly coming back)


Re: Using with Python 3, possible? how?

From
Daniele Varrazzo
Date:
On Sat, Mar 31, 2012 at 11:06 PM, Leon Starr <leon_starr@modelint.com> wrote:
> Greetings all, newbie here.  I'm trying to use the latest release of psycopg2 with Python3 and having no success so
far.
>
> I'm on Mac OS X Lion (Xcode installed) and I have both 2.7 and 3.2.2 Python installations with 3 set as my default
Python
> and these are both macports installations.
>
> So if I fire up my 2.7 IDLE session and do: import psycopg2
> it appears to work okay, i.e. no errors.  (And yes, I am sure it's the 2.7 session)
>
> But the same import in a 3.2.2 IDLE session tells me that no such module exists.
> Well, duh, of course, because psycopg2 is probably installed in the 2.7 site package
> directory.

Yes: Py2 and Py3 installation are separate (FWIW, each 2.x and 3.x is separate)


> My two part question is 1) can I use psycopg2 with Python3?  (The psycopg2 home site leads me to believe so)
> if so 2) How?  Here's what I've tried so far:
>        2a) easy_install would like to install in the 2.7 site install directory, okay, but it's already there, so no
thanks
>                (should I try to force it into the 3.x site installation directory somehow?)

Yes: you should use easy_install provided with "Distribute" installed
on python3: on my system (ubuntu) it is available as the script
"easy_install3".

>        2b) I tried having easy_install load psycopg2 into a separate, empty directory which I put into my PYTHONPATH
>                variable, just to see what would happen.  Didn't work.  (See traceback below)

Of course: it gets linked with the wrong libpython.

>        2c) Should I somehow adjust my package search path in 3.2.2 to look in the 2.7 package directory?
>                What could possibly go wrong with that?  ;)

:) It would go wrong 2b style.


> So, should I be doing a, b, c or none of the above?  I eagerly await enlightenment and thank you for your time!

Either "easy_install3 psycopg2", if you find a way to get Distribute
for Python 3 on your system, or unpack the psycopg source tarball and
run:

    python3 setup.py build
    sudo python3 setup.py install

You will need python-dev and libpq-dev packages installed, but if you
have installed psycopg on py2 via easy_install you should already have
them. Well, you may have py2-dev but not py3-dev... In case of error
check the missing .h at the beginning of the errors list and take a
look at the FAQ to see if they are helpful.

Cheers,

-- Daniele

Re: Using with Python 3, possible? how?

From
Leon Starr
Date:
Thanks, Daniele, you've saved my weekend!  (I think)

The magic word was 'easy_install3'!  That doesn't work on my
installation, but it pointed me in the right direction.

I did a port search on py32 and saw all kinds of relevant goodies.  After installing
distribute and ez_setup, I tried: which easy_install3, but no luck.

So I just did an easy_install again, but this time it fetched and placed correctly, in
the 32 path.  And I was able to import psycopg2 okay from the 3.2.2 session.

BUT, there were a few dozen unsettling warnings generated during the easy_install and I am wondering if this is just
a 2->3 compatibility thing.  How concerned should I be about this kind of stuff?  Excerpt below:

- Leon
=======
psycopg/notify_type.c:226:5: warning: array index of '2' indexes past the end of
      an array (that contains 1 elements) [-Warray-bounds]
    PyTuple_SET_ITEM(args, 2, self->payload);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/local/Library/Frameworks/Python.framework/Versions/3.2/include/python3.2m/tupleobject.h:62:37: note: instantiated from:
#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = v)
                                    ^
/opt/local/Library/Frameworks/Python.framework/Versions/3.2/include/python3.2m/tupleobject.h:27:5: note: 
      array 'ob_item' declared here
    PyObject *ob_item[1];
    ^
In file included from psycopg/notify_type.c:27:
In file included from ./psycopg/psycopg.h:33:
./psycopg/config.h:71:13: warning: unused function 'Dprintf' [-Wunused-function]
static void Dprintf(const char *fmt, ...) {}
            ^
5 warnings generated.
psycopg/xid_type.c:254:9: warning: array index of '1' indexes past the end of an
      array (that contains 1 elements) [-Warray-bounds]
        PyTuple_SET_ITEM(args, 1, self->gtrid);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
========



On Mar 31, 2012, at 4:32 PM, Daniele Varrazzo wrote:

On Sat, Mar 31, 2012 at 11:06 PM, Leon Starr <leon_starr@modelint.com> wrote:
Greetings all, newbie here.  I'm trying to use the latest release of psycopg2 with Python3 and having no success so far.

I'm on Mac OS X Lion (Xcode installed) and I have both 2.7 and 3.2.2 Python installations with 3 set as my default Python
and these are both macports installations.

So if I fire up my 2.7 IDLE session and do: import psycopg2
it appears to work okay, i.e. no errors.  (And yes, I am sure it's the 2.7 session)

But the same import in a 3.2.2 IDLE session tells me that no such module exists.
Well, duh, of course, because psycopg2 is probably installed in the 2.7 site package
directory.

Yes: Py2 and Py3 installation are separate (FWIW, each 2.x and 3.x is separate)


My two part question is 1) can I use psycopg2 with Python3?  (The psycopg2 home site leads me to believe so)
if so 2) How?  Here's what I've tried so far:
       2a) easy_install would like to install in the 2.7 site install directory, okay, but it's already there, so no thanks
               (should I try to force it into the 3.x site installation directory somehow?)

Yes: you should use easy_install provided with "Distribute" installed
on python3: on my system (ubuntu) it is available as the script
"easy_install3".

       2b) I tried having easy_install load psycopg2 into a separate, empty directory which I put into my PYTHONPATH
               variable, just to see what would happen.  Didn't work.  (See traceback below)

Of course: it gets linked with the wrong libpython.

       2c) Should I somehow adjust my package search path in 3.2.2 to look in the 2.7 package directory?
               What could possibly go wrong with that?  ;)

:) It would go wrong 2b style.


So, should I be doing a, b, c or none of the above?  I eagerly await enlightenment and thank you for your time!

Either "easy_install3 psycopg2", if you find a way to get Distribute
for Python 3 on your system, or unpack the psycopg source tarball and
run:

   python3 setup.py build
   sudo python3 setup.py install

You will need python-dev and libpq-dev packages installed, but if you
have installed psycopg on py2 via easy_install you should already have
them. Well, you may have py2-dev but not py3-dev... In case of error
check the missing .h at the beginning of the errors list and take a
look at the FAQ to see if they are helpful.

Cheers,

-- Daniele

-----------------------------------------------------------------------------------------
Model Integration, LLC
Complex Application Requirements Analysis and Modeling
Expert UML/MDA Development, Training and Support

Leon Starr
Analyst / Model Engineer

+1 415 863 8649 office
+1 415 215 9086 cell



912 Cole Street, Suite 161
San Francisco, CA 94117
-----------------------------------------------------------------------------------------






Re: Using with Python 3, possible? how?

From
Daniele Varrazzo
Date:
On Sun, Apr 1, 2012 at 1:54 AM, Leon Starr <leon_starr@modelint.com> wrote:
> Thanks, Daniele, you've saved my weekend!  (I think)

That's great news :)


> BUT, there were a few dozen unsettling warnings generated during the
> easy_install and I am wondering if this is just
> a 2->3 compatibility thing.  How concerned should I be about this kind of
> stuff?

It's a false positive, don't know what compiler is on your system,
probaby clang. I've tried enabling -Warray-bounds on gcc but I don't
get such warnings. I've found discussions about clang -Warray-bounds
being over-zealous:
<http://comments.gmane.org/gmane.comp.compilers.clang.devel/15813>.

The Python tuple structure is defined with a 1-item array ob_item at
the end, but it is initialized with extra trailing memory so that the
other array items "hang off" the structure. It's a classic way of
defining flexible arrays in C structures. And anyway it's in Python
code, not in Psycopg's.

Cheers,


-- Daniele

Re: Using with Python 3, possible? how?

From
Leon Starr
Date:
Thanks for the reference and info, Daniele.  Yes, clang on my end.  I've been able to connect to my db and
interact with my plpgsql functions okay, so no trouble yet.  But I will research a bit further.

Now I just have the minor detail of re-absorbing Python as I haven't written any since about 2006!
It's all coming back…

Again, big thanks as I am sure I would still be banging my head if you hadn't pointed out that I was
using the wrong install program/version since that would have been too simple and obvious a solution
to consider.  Ha!

- Leon

On Apr 1, 2012, at 9:46 AM, Daniele Varrazzo wrote:

> On Sun, Apr 1, 2012 at 1:54 AM, Leon Starr <leon_starr@modelint.com> wrote:
>> Thanks, Daniele, you've saved my weekend!  (I think)
>
> That's great news :)
>
>
>> BUT, there were a few dozen unsettling warnings generated during the
>> easy_install and I am wondering if this is just
>> a 2->3 compatibility thing.  How concerned should I be about this kind of
>> stuff?
>
> It's a false positive, don't know what compiler is on your system,
> probaby clang. I've tried enabling -Warray-bounds on gcc but I don't
> get such warnings. I've found discussions about clang -Warray-bounds
> being over-zealous:
> <http://comments.gmane.org/gmane.comp.compilers.clang.devel/15813>.
>
> The Python tuple structure is defined with a 1-item array ob_item at
> the end, but it is initialized with extra trailing memory so that the
> other array items "hang off" the structure. It's a classic way of
> defining flexible arrays in C structures. And anyway it's in Python
> code, not in Psycopg's.
>
> Cheers,
>
>
> -- Daniele