Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AttributeError: module 'itertools' has no attribute 'imap'

84 views
Skip to first unread message

Larry Martell

unread,
Aug 8, 2019, 10:30:30 AM8/8/19
to
I have some code that is using the pyke package
(https://sourceforge.net/projects/pyke/). That project seems fairly
dead, so asking here.

There is a pyke function that returns a context manager with an
iterable map. In py2.7 I did this:

from pyke import knowledge_engine
vasculopathy_engine =
knowledge_engine.engine((rule_base_source_folder,
(compiled_rule_base_folder)))
with vasculopathy_engine.prove_goal(...) as presentationGen:
for vals, plan in presentationGen:

But in py3 that fails with: AttributeError: module 'itertools' has no
attribute 'imap'

I tried converting presentationGen to a list but get the same error.

How can I make this work in py3?

Paul Rubin

unread,
Aug 8, 2019, 11:15:44 AM8/8/19
to
Larry Martell <larry....@gmail.com> writes:
> But in py3 that fails with: AttributeError: module 'itertools' has no
> attribute 'imap'
> How can I make this work in py3?

In py3 the built-in map function creates an iterator rather than a list,
so it does what py2's imap does. Leaving the redundant copy in py3
itertools would have avoided some gratuitous code breakage of course.

Peter Otten

unread,
Aug 8, 2019, 11:30:08 AM8/8/19
to
Larry Martell wrote:

> I have some code that is using the pyke package
> (https://sourceforge.net/projects/pyke/). That project seems fairly
> dead, so asking here.
>
> There is a pyke function that returns a context manager with an
> iterable map. In py2.7 I did this:
>
> from pyke import knowledge_engine
> vasculopathy_engine =
> knowledge_engine.engine((rule_base_source_folder,
> (compiled_rule_base_folder)))
> with vasculopathy_engine.prove_goal(...) as presentationGen:
> for vals, plan in presentationGen:
>
> But in py3 that fails with: AttributeError: module 'itertools' has no
> attribute 'imap'

In Python 3 the map() builtin is "lazy", so you can use that instead.

> I tried converting presentationGen to a list but get the same error.
>
> How can I make this work in py3?

The problem is in the project rather than in your code -- you have to port
pyke to Python 3 before you can use it. If you want to go that route you may
give the 2to3 tool a try:

$ cat demo.py
import itertools

for i in itertools.imap(abs, [-1, 1]):
print i
$ 2to3 -w demo.py
[...]
$ cat demo.py
import itertools

for i in map(abs, [-1, 1]):
print(i)
$


MRAB

unread,
Aug 8, 2019, 11:55:42 AM8/8/19
to
On 2019-08-08 15:29, Larry Martell wrote:
> I have some code that is using the pyke package
> (https://sourceforge.net/projects/pyke/). That project seems fairly
> dead, so asking here.
>
> There is a pyke function that returns a context manager with an
> iterable map. In py2.7 I did this:
>
> from pyke import knowledge_engine
> vasculopathy_engine =
> knowledge_engine.engine((rule_base_source_folder,
> (compiled_rule_base_folder)))
> with vasculopathy_engine.prove_goal(...) as presentationGen:
> for vals, plan in presentationGen:
>
> But in py3 that fails with: AttributeError: module 'itertools' has no
> attribute 'imap'
>
> I tried converting presentationGen to a list but get the same error.
>
> How can I make this work in py3?
>
The built-in 'map' in Python 3 does what 'imap' did in Python 2.

Larry Martell

unread,
Aug 8, 2019, 12:17:30 PM8/8/19
to
On Thu, Aug 8, 2019 at 11:30 AM Peter Otten <__pet...@web.de> wrote:
>
> Larry Martell wrote:
>
> > I have some code that is using the pyke package
> > (https://sourceforge.net/projects/pyke/). That project seems fairly
> > dead, so asking here.
> >
> > There is a pyke function that returns a context manager with an
> > iterable map. In py2.7 I did this:
> >
> > from pyke import knowledge_engine
> > vasculopathy_engine =
> > knowledge_engine.engine((rule_base_source_folder,
> > (compiled_rule_base_folder)))
> > with vasculopathy_engine.prove_goal(...) as presentationGen:
> > for vals, plan in presentationGen:
> >
> > But in py3 that fails with: AttributeError: module 'itertools' has no
> > attribute 'imap'
>
> In Python 3 the map() builtin is "lazy", so you can use that instead.
>
> > I tried converting presentationGen to a list but get the same error.
> >
> > How can I make this work in py3?
>
> The problem is in the project rather than in your code -- you have to port
> pyke to Python 3 before you can use it. If you want to go that route you may
> give the 2to3 tool a try:
>
> $ cat demo.py
> import itertools
>
> for i in itertools.imap(abs, [-1, 1]):
> print i
> $ 2to3 -w demo.py
> [...]
> $ cat demo.py
> import itertools
>
> for i in map(abs, [-1, 1]):
> print(i)
> $

Pyke has been ported to py3. Here is the code that returns the data I
am trying to process:

return map(self.doctor_answer, it)

I don't see anything calling imap.

Larry Martell

unread,
Aug 8, 2019, 12:25:47 PM8/8/19
to
I grepped through the entire pyke code and imap is not in there.

Rhodri James

unread,
Aug 8, 2019, 12:26:44 PM8/8/19
to
On 08/08/2019 17:16, Larry Martell wrote:
> On Thu, Aug 8, 2019 at 11:30 AM Peter Otten <__pet...@web.de> wrote:
>>
>> Larry Martell wrote:
[snip]
>>> But in py3 that fails with: AttributeError: module 'itertools' has no
>>> attribute 'imap'
>>
>> In Python 3 the map() builtin is "lazy", so you can use that instead.
>>
>>> I tried converting presentationGen to a list but get the same error.
>>>
>>> How can I make this work in py3?
>>
>> The problem is in the project rather than in your code -- you have to port
>> pyke to Python 3 before you can use it.
[snip]
>
> Pyke has been ported to py3. Here is the code that returns the data I
> am trying to process:
>
> return map(self.doctor_answer, it)
>
> I don't see anything calling imap.

That suggests you aren't calling the code you think you're calling. I
think a little poking with pdb or some strategic prints is in order...

--
Rhodri James *-* Kynesim Ltd

Larry Martell

unread,
Aug 8, 2019, 12:46:30 PM8/8/19
to
I traced through the code with pdb. It is running from
/usr/local/lib/python3.5/dist-packages/pyke and I grepped that for
imap. Nothing

Peter Otten

unread,
Aug 8, 2019, 1:33:08 PM8/8/19
to
Larry Martell wrote:

>> Pyke has been ported to py3. Here is the code that returns the data I
>> am trying to process:
>>
>> return map(self.doctor_answer, it)
>>
>> I don't see anything calling imap.
>
> I grepped through the entire pyke code and imap is not in there.

Fire up the python3 interpreter that you use to run your script:

>>> import pyke
>>> pyke.__file__
'/somewhere/pyke/__init__.py'

Then grep starting in the directory shown above and be enlightened ;)

If I'm wrong here's an alternative guess (I know nothing about pyke):

knowledge_engine.engine((rule_base_source_folder,
(compiled_rule_base_folder)))

Are the rules "compiled" into Python? Then Make sure that you use separate
`compiled_rule_base_folder`s for Python 2 and 3.

Larry Martell

unread,
Aug 8, 2019, 1:52:09 PM8/8/19
to
I figured it out - pyke creates python code and the code created by
the py2 version of pyke was still there and being used. Once I deleted
those the p3y pyke recreated them and it worked. Thanks!
0 new messages