为什么这个python脚本找不到libclang dll?

2024-10-01 11:40:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想开始使用libclangPython。我正在尝试获取一个示例代码(http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/)来运行Windows,下面是我尝试运行的代码的一部分:

#!/usr/bin/python
# vim: set fileencoding=utf-8

import sys
import os
import clang.cindex
import itertools

...

print("Setting clang path")
# I tried multiple variations. Libclang is correctly installed in the specified location.
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin')
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin/libclang.dll')

# I also tried moving the dll into the Python installation folder.
clang.cindex.Config.set_library_file('C:/Python27/DLLs/libclang.dll')
print("Clang path set")

index = clang.cindex.Index.create()

...

我删除了代码的所有其他部分,但是如果它们是相关的,我可以发布它们。线

^{pr2}$

引发以下错误:

Setting clang path
Clang path set
Traceback (most recent call last):
  File "D:\libclangtest\boost_python_gen.py", line 60, in <module>
    index = clang.cindex.Index.create()
  File "D:\libclangtest\clang\cindex.py", line 2095, in create
    return Index(conf.lib.clang_createIndex(excludeDecls, 0))
  File "D:\libclangtest\clang\cindex.py", line 141, in __get__
    value = self.wrapped(instance)
  File "D:\libclangtest\clang\cindex.py", line 3392, in lib
    lib = self.get_cindex_library()
  File "D:\libclangtest\clang\cindex.py", line 3423, in get_cindex_library
    raise LibclangError(msg)
clang.cindex.LibclangError: [Error 193] %1 is not a valid Win32 application. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().

这是什么原因?我是否设置了错误的dll路径?我尝试了多种方法,使用前斜杠和反斜杠,我还试图将dll从程序文件中移出,使路径不包含空格,但没有任何效果。在

我完全是libclang和Python的初学者,如果我问的是一些琐碎的问题,我会很抱歉。在


Tags: path代码inpyimportconfiglinelibrary
2条回答

我也遇到了类似的问题(Windows7x64,Anaconda3x64)。使用

clang.cindex.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')

解决了这个问题。请注意,您需要使用斜杠(而不是反斜杠),并指定到bin的路径/libclang.dll(非lib/libclang.dll). 在

@SK logic评论说,我应该检查Python和libclang是否是32位还是64位。Libclang是32位的,但是我找不到一种方法来检查我的Python安装是32位还是64位,所以我重新安装了32位版本,现在它可以工作了。可能是64位的Python版本有问题。在

相关问题 更多 >