How to solve "NameError: name 'basestring' is not defined" when importing psycopg2?

Question:

I am trying to import the psycopg2 package on python 3.7.13 but I get the following error:

python -c "import psycopg2"                                                                    % 11:22:56
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/mm/.pyenv/versions/3.7.13/lib/python3.7/site-packages/psycopg2/__init__.py", line 68, in <module>
    from psycopg2 import extensions as _ext
  File "/Users/mm/.pyenv/versions/3.7.13/lib/python3.7/site-packages/psycopg2/extensions.py", line 211, in <module>
    from psycopg2. _range import Range                              # noqa
  File "/Users/mm/.pyenv/versions/3.7.13/lib/python3.7/site-packages/psycopg2/_range.py", line 503, in <module>
    oid=3904, subtype_oid=23, array_oid=3905)
  File "/Users/mm/.pyenv/versions/3.7.13/lib/python3.7/site-packages/psycopg2/_range.py", line 283, in __init__
    self._create_ranges(pgrange, pyrange)
  File "/Users/mm/.pyenv/versions/3.7.13/lib/python3.7/site-packages/psycopg2/_range.py", line 302, in _create_ranges
    if isinstance(pgrange, basestring):
NameError: name 'basestring' is not define

I am wondering how can I solve it ?

I already tried downgrading pip to 20.3 after seeing this issue https://github.com/psycopg/psycopg2/issues/1419.

Asked By: fzk

||

Answers:

Resolved this by manually transforming psycopg2 package to a python 3 compatible version running:

pip install 2to3
2to3 ~/.pyenv/versions/3.7.13/lib/python3.7/site-packages/psycopg2/ -w 

Still have no idea why this error occured though.

Answered By: fzk
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.