2016-04-24 3 views
3

Folgende Eli's und glehmann's Führungen;Kann libclang nicht ausführen: Fehler '' spezifiziertes Modul konnte nicht gefunden werden ''

(unter Windows)

installed LLVM-3.8.0-win32 from here

installed libclang-py3 package (version 0.3)

added C:\Program Files (x86)\LLVM\bin\libclang.dll to my Path environment variables

Wenn ich versuche, unter dem Code auszuführen (aus den Führungen genommen, die ich oben erwähnt)

clang.cindex.Config.set_library_path('C:\Program Files (x86)\LLVM\bin\libclang.dll') 

def find_typerefs(node, typename): 
    # Find all references to the type named 'typename' 
    if node.kind.is_reference(): 
     ref_node = clang.cindex.Cursor_ref(node) 
     if ref_node.spelling == typename: 
      print('Found %s [line=%s, col=%s]' % (
       typename, node.location.line, node.location.column)) 
    # Recurse for children of this node 
    for c in node.get_children(): 
     find_typerefs(c, typename) 

index = clang.cindex.Index.create() 
tu = index.parse(cppsource) 
print('Translation unit:', tu.spelling) 
find_typerefs(tu.cursor, 'Person') 

ich die folgenden Fehlermeldung erhalten:

C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/Duca/Desktop/PyPlag/rough-scripts/c++_parser_with_libclang.py 
Traceback (most recent call last): 
    File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 3623, in get_cindex_library 
    library = cdll.LoadLibrary(self.get_filename()) 
    File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\ctypes\__init__.py", line 425, in LoadLibrary 
    return self._dlltype(name) 
    File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\ctypes\__init__.py", line 347, in __init__ 
    self._handle = _dlopen(self._name, mode) 
OSError: [WinError 126] The specified module could not be found 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:/Users/Duca/Desktop/PyPlag/rough-scripts/c++_parser_with_libclang.py", line 49, in <module> 
    index = clang.cindex.Index.create() 
    File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 2238, in create 
    return Index(conf.lib.clang_createIndex(excludeDecls, 0)) 
    File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 141, in __get__ 
    value = self.wrapped(instance) 
    File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 3592, in lib 
    lib = self.get_cindex_library() 
    File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 3628, in get_cindex_library 
    raise LibclangError(msg) 
clang.cindex.LibclangError: [WinError 126] The specified module could not be found. To provide a path to libclang use Config.set_library_path() or Config.set_library_file(). 

Process finished with exit code 1 

Auch versucht,

downloaded cfe-3.8.0.src.tar from here and copied the clang.cindex module inside my script's directory

installed LLVM-3.8.0-win64 and updated the Path environment variable as well as the clang.cindex.Config.set_library_path statement (after reading this question)

copied the libclang.dll from the installation dir to my Python DLLs dir

used clang.cindex.Config.set_library_path('C:\Program Files (x86)\LLVM\bin') and clang.cindex.Config.set_library_file('C:\Program Files (x86)\LLVM\bin\libclang.dll') and clang.cindex.Config.set_library_file('C:\Program Files (x86)\LLVM\bin')

ohne Erfolg.

+0

Haben Sie versucht, set_library_file anstatt set_library_path? –

+0

Ja, ich tat genau denselben Fehler. :( –

+2

Es sieht aus wie Sie ein paar mögliche Probleme haben. 1) Sie sind nicht sicher, warum die DLL gefunden wird; 2) Sie sind nicht sicher, ob Sie eine kompatible Reihe von Python-Bindungen haben. Die Python-Bindings (alle von ihnen) enthalten genug Funktionalität, um das zu tun, was Sie erhoffen (also ist es kein Problem) - ich bin zuversichtlich, weil die Implementierung auf Ctypes basiert. Wenn das die DLL-Problem kann Sie die cindex.py-Datei manuell bearbeiten, um genau zu drucken, welche Datei geöffnet wird, wenn Sie auch os.path.exists behaupten, erhalten Sie möglicherweise ein wenig mehr Einblick in was schief geht –

Antwort

5

Ich fürchte, der Hauptschuldige in Ihrem Code ist die rückwärts Schrägstrich. Sie müssen die clang.cindex.Config.set_library_path('C:\Program Files (x86)\LLVM\bin\libclang.dll')zuclang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll') ändern.

Alternativ, und ich glaube, es ist eine bessere Programmierpraxis, können Sie die os.sep sowohl Windows-und Linux-Pfad Trennzeichen Fälle behandeln.

Außerdem gibt es ein anderes Problem in Ihrem Code; Sie müssen nämlich die ref_node = clang.cindex.Cursor_ref(node)zuref_node = node.get_definition() ändern, um eine AttributeError zu vermeiden, da Cursor_ref nicht länger ein Attribut des Moduls clang.cindex ist.

Nach der obigen Festsetzung läuft mit den Parametern simple_demo_src.cpp Person Sie keine Fehler bekommen und zu sehen, diesen Ausgang werden sollten:

Translation unit: simple_demo_src.cpp 
Found Person [line=7, col=21] 
Found Person [line=13, col=5] 
Found Person [line=24, col=5] 
Found Person [line=24, col=21] 
Found Person [line=25, col=9] 

das ist genau das, was Eli auf seinem page erwähnt.

+1

Benötigt so viel, danke! Der "os.sep" ist ein guter Rat. Und danke, dass du auch den 'AttributError' aufgehoben hast. –

Verwandte Themen