hodowcanietoperzy
hodowcanietoperzy

Reputation: 3

How to import pairwise function from itertools? Do I need to update itertools?

I have a problem with updating itertools package, so that the pairwise function is available.

I am getting this error:

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

Additionally, this command:

print(itertools.__version__)

returns:

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

and I do not know why.

How to check the version of itertools that I have? And how to update this package so I can have the pairwise function?

I tried to update the itertools by myself but I failed.

I update python to 3.11. When I type in termnal:

python3.11 --version

I get:

Python 3.11.0

Upvotes: -1

Views: 2453

Answers (2)

Nikolaj Š.
Nikolaj Š.

Reputation: 1996

Looks like you're trying to import a function as if it was a package:

 $ python3.11 -c 'import itertools.pairwise'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'itertools.pairwise'; 'itertools' is not a package

This is how it's done

from itertools import pairwise
pairwise(...)
# OR
import itertools
itertools.pairwise(...)

Upvotes: 2

Looks like in my version of python, thats give that same error, itertools doesnt have the pairwise function any more. To get my code running I change from itertools import pairwise to from more_itertools import pairwise

Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:56:21) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> dir(itertools)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', '_grouper', '_tee', '_tee_dataobject', 'accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']

Upvotes: -2

Related Questions