Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using re2 in Python3

I need to use re2 in python3. The installation worked fine but when I import it I receive this error:

>>> import re2 as re
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "re2.pyx", line 1, in init re2 (src/re2.cpp:13681)
NameError: basestring

Does anyone know what the problem is?

like image 603
user1829243 Avatar asked Dec 13 '22 20:12

user1829243


1 Answers

The version released to PyPI is not Python 3 compatible; basestring only exists in Python 2. This will not be the only problem, fixing a text-focused project to fit the Python 3 all-text-is-Unicode view is not trivial.

It appears the specific project is unmaintained; others have already reported the problem, and people have pointed to a different fork: https://github.com/andreasvc/pyre2.

You can install that project directly from GitHub:

pip install git+https://github.com/andreasvc/pyre2.git

Note that you'll need to install Cython first for that project to compile; unlike the other fork the generated C++ file (from the re2.pyx file) is not checked in. Just run pip install Cython.

You could also look at alternatives; perhaps the regex module would fit your requirements as well. regex is a drop-in replacement for re with additional features, like vastly improved Unicode support.

like image 194
Martijn Pieters Avatar answered Dec 27 '22 15:12

Martijn Pieters