Unable to import clang bindings in Anaconda/Python3

2.3k views Asked by At

I just pip installed clang to my Anaconda Python 3 installation and I'm unable to import the clang python bindings...

C:\Temp>python -m pip install clang --proxy="xxxxx"
Collecting clang
  Downloading clang-3.8.tar.gz
Building wheels for collected packages: clang
  Running setup.py bdist_wheel for clang ... done
  Stored in directory: C:\xxxxxx
Successfully built clang
Installing collected packages: clang
Successfully installed clang-3.8

C:\Temp>python
>>> import clang.cindex
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Anaconda3\lib\site-packages\clang\cindex.py", line 515
        raise ValueError,'{0} value {1} already loaded'.format(
                        ^
    SyntaxError: invalid syntax

I am however able to successfully import clang by itself, although not with anything useful...

>>> import clang
>>> clang.
clang.__all__           clang.__loader__        clang.__spec__          clang.__dir__(          clang.__getattribute__( clang.__le__(           clang.__reduce__(       clang.__sizeof__(
clang.__cached__        clang.__name__          clang.__class__(        clang.__eq__(           clang.__gt__(           clang.__lt__(           clang.__reduce_ex__(    clang.__str__(
clang.__doc__           clang.__package__       clang.__delattr__(      clang.__format__(       clang.__hash__(         clang.__ne__(           clang.__repr__(         clang.__subclasshook__(
clang.__file__          clang.__path__          clang.__dict__          clang.__ge__(           clang.__init__(         clang.__new__(          clang.__setattr__(

Looking at the source file where the exception is thrown appears to suggest it may be a Python3 syntax issue...

### Cursor Kinds ###
class BaseEnumeration(object):
    """
    Common base class for named enumerations held in sync with Index.h values.

    Subclasses must define their own _kinds and _name_map members, as:
    _kinds = []
    _name_map = None
    These values hold the per-subclass instances and value-to-name mappings,
    respectively.

    """

    def __init__(self, value):
        if value >= len(self.__class__._kinds):
            self.__class__._kinds += [None] * (value - len(self.__class__._kinds) + 1)
        if self.__class__._kinds[value] is not None:
            raise ValueError,'{0} value {1} already loaded'.format( #<--Py2 Syntax                    str(self.__class__), value)
        self.value = value
        self.__class__._kinds[value] = self
        self.__class__._name_map = None

Looking at the rest of cindex.py seems consistent that the libclang bindings have not yet been developed for Python3. Is that accurate or did my pip or something else get mixed up during the install?

2

There are 2 answers

0
jxramos On BEST ANSWER

Looks like it was a Py2/3 syntax issue, luckily I was able to run 2to3.exe on the folder C:\Anaconda3\Lib\site-packages\clang and merge over the fixes

cindex.py diff results Python 2 vs 3 syntax changes from 2to3.exe

Now I'm in business...

>>> import clang.cindex
>>> clang.cindex.
clang.cindex.ARRAY(                    clang.cindex.LibclangError(            clang.cindex.__doc__                   clang.cindex.c_uint64(                 clang.cindex.set_last_error(
clang.cindex.AccessSpecifier(          clang.cindex.LibraryLoader(            clang.cindex.__file__                  clang.cindex.c_uint8(                  clang.cindex.sizeof(
clang.cindex.ArgumentError(            clang.cindex.LittleEndianStructure(    clang.cindex.__loader__                clang.cindex.c_ulong(                  clang.cindex.string_at(
clang.cindex.Array(                    clang.cindex.OleDLL(                   clang.cindex.__name__                  clang.cindex.c_ulonglong(              clang.cindex.util
clang.cindex.BaseEnumeration(          clang.cindex.POINTER(                  clang.cindex.__package__               clang.cindex.c_ushort(                 clang.cindex.windll
clang.cindex.BigEndianStructure(       clang.cindex.PYFUNCTYPE(               clang.cindex.__spec__                  clang.cindex.c_void_p(                 clang.cindex.wintypes
clang.cindex.CCRStructure(             clang.cindex.PyDLL(                    clang.cindex.addressof(                clang.cindex.c_voidp(                  clang.cindex.wstring_at(
clang.cindex.CDLL(                     clang.cindex.RTLD_GLOBAL               clang.cindex.alignment(                clang.cindex.c_wchar(                  clang.cindex.__class__(
clang.cindex.CFUNCTYPE(                clang.cindex.RTLD_LOCAL                clang.cindex.availabilityKinds         clang.cindex.c_wchar_p(                clang.cindex.__delattr__(
clang.cindex.CachedProperty(           clang.cindex.RefQualifierKind(         clang.cindex.byref(                    clang.cindex.callbacks                 clang.cindex.__dict__
clang.cindex.ClangObject(              clang.cindex.SetPointerType(           clang.cindex.c_bool(                   clang.cindex.cast(                     clang.cindex.__dir__(
clang.cindex.CodeCompletionResult(     clang.cindex.SourceLocation(           clang.cindex.c_buffer(                 clang.cindex.cdll                      clang.cindex.__eq__(
clang.cindex.CodeCompletionResults(    clang.cindex.SourceRange(              clang.cindex.c_byte(                   clang.cindex.clang                     clang.cindex.__format__(
clang.cindex.CompilationDatabase(      clang.cindex.SpellingCache             clang.cindex.c_char(                   clang.cindex.collections               clang.cindex.__ge__(
clang.cindex.CompilationDatabaseError( clang.cindex.StorageClass(             clang.cindex.c_char_p(                 clang.cindex.completionChunkKindMap    clang.cindex.__getattribute__(
clang.cindex.CompileCommand(           clang.cindex.Structure(                clang.cindex.c_double(                 clang.cindex.conf                      clang.cindex.__gt__(
clang.cindex.CompileCommands(          clang.cindex.TemplateArgumentKind(     clang.cindex.c_float(                  clang.cindex.create_string_buffer(     clang.cindex.__hash__(
clang.cindex.CompletionChunk(          clang.cindex.Token(                    clang.cindex.c_int(                    clang.cindex.create_unicode_buffer(    clang.cindex.__init__(
clang.cindex.CompletionString(         clang.cindex.TokenGroup(               clang.cindex.c_int16(                  clang.cindex.functionList              clang.cindex.__le__(
clang.cindex.Config(                   clang.cindex.TokenKind(                clang.cindex.c_int32(                  clang.cindex.get_errno(                clang.cindex.__lt__(
clang.cindex.Cursor(                   clang.cindex.TranslationUnit(          clang.cindex.c_int64(                  clang.cindex.get_last_error(           clang.cindex.__ne__(
clang.cindex.CursorKind(               clang.cindex.TranslationUnitLoadError( clang.cindex.c_int8(                   clang.cindex.memmove(                  clang.cindex.__new__(
clang.cindex.DEFAULT_MODE              clang.cindex.TranslationUnitSaveError( clang.cindex.c_long(                   clang.cindex.memset(                   clang.cindex.__reduce__(
clang.cindex.Diagnostic(               clang.cindex.Type(                     clang.cindex.c_longdouble(             clang.cindex.oledll                    clang.cindex.__reduce_ex__(
clang.cindex.DllCanUnloadNow(          clang.cindex.TypeKind(                 clang.cindex.c_longlong(               clang.cindex.pointer(                  clang.cindex.__repr__(
clang.cindex.DllGetClassObject(        clang.cindex.Union(                    clang.cindex.c_object_p(               clang.cindex.py_object(                clang.cindex.__setattr__(
clang.cindex.File(                     clang.cindex.WINFUNCTYPE(              clang.cindex.c_short(                  clang.cindex.pydll                     clang.cindex.__sizeof__(
clang.cindex.FileInclusion(            clang.cindex.WinDLL(                   clang.cindex.c_size_t(                 clang.cindex.pythonapi                 clang.cindex.__str__(
clang.cindex.FixIt(                    clang.cindex.WinError(                 clang.cindex.c_ssize_t(                clang.cindex.register_enumerations(    clang.cindex.__subclasshook__(
clang.cindex.FormatError(              clang.cindex._CXString(                clang.cindex.c_ubyte(                  clang.cindex.register_function(
clang.cindex.GetLastError(             clang.cindex._CXUnsavedFile(           clang.cindex.c_uint(                   clang.cindex.register_functions(
clang.cindex.HRESULT(                  clang.cindex.__all__                   clang.cindex.c_uint16(                 clang.cindex.resize(
clang.cindex.Index(                    clang.cindex.__cached__                clang.cindex.c_uint32(                 clang.cindex.set_errno(
2
Mikaelblomkvistsson On

How the heck 'pip' which I always used for python2.7 became pip for python3?

$ pip --version
pip 1.5.6 from /usr/lib/python3/dist-packages (python 3.4)

But then the setup has been ran by python2.7, causing syntax error. Magic.

I called the same, just but ensuring pip for python2.7:

pip2 install clang

and it worked nicely.