Why can't this python script find the libclang dll? - python

I would like to get started with using libclang with Python. I am trying to get a sample code (http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/) to work on Windows, here is a part of the code I'm trying to run:
#!/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()
...
I stripped all the other parts of the code, but I can post them if they are relevant. The line
index = clang.cindex.Index.create()
Throws the following error:
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().
What is the reason for this? Am I setting the dll's path wrong? I tried multiple ways, with foreslashes and backslashes, I also tried to move the dll out of Program Files to make the path contain no spaces, but nothing worked.
I am a total beginner to libclang and Python, sry if I'm asking something trivial.

I was running into a similar problem (Windows 7 x64, Anaconda3 x64). Using
import clang.cindex
clang.cindex.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')
fixed the problem. Please note that you need to use slashes (not antislashes), and specify path to bin/libclang.dll (not to lib/libclang.dll).

#SK-logic commented that I should check whether both Python and libclang are either 32bit or 64bit. Libclang was 32bit, but I couldn't find a way to check whether my Python installation is 32 or 64, so I reinstalled the 32bit version, and now it works. So the problem probably was that I had the 64 bit version of Python.

Related

Import darknet FileNotFoundError: Could not find module

I am getting this error when I run import darknet:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\darknet-master\build\darknet\x64\darknet.py", line 211, in <module>
lib = CDLL(winGPUdll, RTLD_GLOBAL)
File "C:\Users\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__
self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'C:\Users\darknet-master\build\darknet\x64\yolo_cpp_dll.dll' (or one of its dependencies). Try using the full path with constructor syntax.```
Instead of just downloading yet another version of python (yawn), we can fix darknet.py to import the DLLs correctly. As mentioned here, we need to add correct DLL import paths if using python > 3.8. The solution is to add these lines
os.add_dll_directory('c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin')
os.add_dll_directory(os.path.dirname(__file__))
somewhere before the CDLL calls. You can place them at the top of the script.
The first line allows python to load DLLs from your CUDA install which you need if you're using GPU.
The second line allows python to load DLLs from the current working directory (into which you should copy yolo_cpp_dll.dll and pthreadVC2.dll). Alternatively you could replace this to the path that contains these DLLs.
lib = CDLL(winGPUdll, RTLD_GLOBAL, winmode=0)
Add the winmode=0 if python > 3.8
This is because Python 3.9 has some issue. It throws an error when the yolo_cpp_dll.dll file is imported.
Here's how to fix it:
Install Python 3.7 or lower (3.6 worked for me). You can either install version 3.7 along with your current version or remove the current one and install Python 3.7 only. If installed alongside current version then you'll have to rename the newly installed python (3.7) exe file to something like python3.7 in its installed location.
After renaming, add that directory to the PATH environment variable in Windows.
Go to your file location where darknet is stored, (for me it's C:\Users\ARYA\Documents\Penelitian1\coba1_darknet\darknet-master\build\darknet\x64\), open the Command Prompt and type python3.7 (rather than python only). This will open Python 3.7.
Here you can now run import darknet.
The answer above of #xiang zhang
worked for me by adding winmod=0 in the lines 214, 218 and 222.
if not os.path.exists(winGPUdll):
raise ValueError("NoDLL")
lib = CDLL(winGPUdll, RTLD_GLOBAL, winmode=0)
except (KeyError, ValueError):
hasGPU = False
if os.path.exists(winNoGPUdll):
lib = CDLL(winNoGPUdll, RTLD_GLOBAL, winmode=0)
print("Notice: CPU-only mode")
else:
# Try the other way, in case no_gpu was compile but not renamed
lib = CDLL(winGPUdll, RTLD_GLOBAL, winmode=0)

Treepoem barcode generator unable to find ghostscript

I'm trying to generate a pdf417 barcode in python using treepoem but pycharm keeps giving me the following error:
Traceback (most recent call last):
File "C:/Users/./Documents/barcodes.py", line 175, in
image = generate_barcode(barcode_type="pdf417",data=barcode, options=dict(eclevel=5, rows=27, columns=12))
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 141, in generate_barcode
bbox_lines = _get_bbox(code)
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 81, in _get_bbox
ghostscript = _get_ghostscript_binary()
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 108, in _get_ghostscript_binary
'Cannot determine path to ghostscript, is it installed?'
treepoem.TreepoemError: Cannot determine path to ghostscript, is it installed?
I've tried to install ghostcript, using both the .exe I found online and using pip install ghostscript (successfully completed the first time, and now tells me the requirement is satisfied), yet I still keep getting this error. Any ideas on how to fix it?
You are installing on Windows, the Windows binary differs in name from the Linux binaries and indeed differs depending whether you installed the 64 or 32-bit version.
On Linux (and MacOS) the Ghostscript binary is called 'gs', on Windows its 'gswin32' or 'gswin64' or 'gswin32c' or 'gswin64c' depending on whether you want the 32 or 64 bit version, and the command line or windowed executable.
My guess is that your script is looking for simply 'gs' and is probably expecting the path to be in the $PATH environment variable, its not clear to me what its expecting.
You could probably 'fix' this by making sure the installation path is in the $PATH environment variable and copying the executable to 'gs.exe' in that directory.
Other than that you'll need someone who can tell you what the script is looking for. Quite possibly you could just grep it.
Another solution is to edit the C:\Users\Windows.UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\treepoem__init__.py
the script is looking for gs.exe, change to gswin32.exe as shown below.
Then add the GhostScriptInstallDir\bin in the PATH in windows.
def _get_ghostscript_binary():
binary = "gswin32" # changed from 'gs' to 'gswin32'
if sys.platform.startswith("win"):
binary = EpsImagePlugin.gs_windows_binary
if not binary:
raise TreepoemError(
"Cannot determine path to ghostscript, is it installed?"
)
return binary

Configure pyglfw

I am trying to create an OpenGL context using Python. I am attempting to use the python bindings for GLFW but I am having trouble getting them working. I found the bindings at https://github.com/rougier/pyglfw from the GLFW main page.
I am getting the following error when I run my test program:
python HelloOpenGL.py
Traceback (most recent call last):
File "HelloOpenGL.py", line 2, in <module>
import glfw #Windowing Toolkit - GLFW
File "C:\<...>\glfw.py", line 60, in <module>
raise OSError('GLFW library not found')
OSError: GLFW library not found
I suspect that I need a glfw dll (I could be wrong). I have tried copying over the dll I use for C++ GLFW but I get the same error. I have tried both the 32 and 64 bit dlls for GLFW 3.1 compiled with the GNU compiler. I am using a Windows 10 64 bit OS and Python 3.4.
I also came across this question: Configuring glfw for Python in Eclipse. The answer is particularly unhelpful as the problem is not to do with installing pyglfw but setting up other dependencies. I did use pip to install pyglfw initially but it did not work correctly and python couldn't find the module; I have installed pyglfw manually and it is working.
Question: can someone provide instructions for setting up pyglfw? I have been unable to find anything relevant. I need to know what dependencies are needed to make it work as well.
Here is the test program:
import OpenGL.GL as gl #OpenGL
import glfw #Windowing Toolkit - GLFW
glfw.init()
if (glfw.OpenWindow(800, 600, 5, 6, 5, 0, 8, 0, glfw.FULLSCREEN) != True):
glfw.Terminate(); # calls glfwTerminate() and exits
glfw.SetWindowTitle("The GLFW Window");
I just figured this out a few seconds ago, and it turns out that on 64-bit systems with 32 bit python, you need to put the DLL in C:\Windows\SysWOW64, then python can find it.
I opened up the pyglfw module. This is a problem that will occur on Windows systems because of the way the module searches for the GLFW DLL. The module searches for the library path using ctypes.util.find_library(), which searches directories in the PATH environment variable, and not the working directory.
The solution for me was to hard-code the DLL in pyglfw. This can be done with the following code:
_glfw = ctypes.WinDLL('glfw3')
This will now load the glfw3.dll so long as it is placed in the same directory. (For older versions of GLFW the DLL is glfw.dll)
This code should replace lines 45-53 in the original code:
# First if there is an environment variable pointing to the library
if 'GLFW_LIBRARY' in os.environ:
if os.path.exists(os.environ['GLFW_LIBRARY']):
_glfw_file = os.path.realpath(os.environ['GLFW_LIBRARY'])
# Else, try to find it
if _glfw_file is None:
order = ['glfw', 'glfw3']
for check in order:
_glfw_file = ctypes.util.find_library(check)
if _glfw_file is not None:
break
# Else, we failed and exit
if _glfw_file is None:
raise OSError('GLFW library not found')
# Load it
_glfw = ctypes.CDLL(_glfw_file)
This question: find_library() in ctypes details the solutions to loading libraries on windows.
This outlines another solution, which would be to set the search path at runtime:
You can add the DLL directory to PATH dynamically at runtime (in
contrast to the Linux loader's caching of LD_LIBRARY_PATH at startup).
For example, say your DLL dependencies are in the "dlls" subdirectory
of your package. You can prepend this directory as follows:
import os
basepath = os.path.dirname(os.path.abspath(__file__))
dllspath = os.path.join(basepath, 'dlls')
os.environ['PATH'] = dllspath + os.pathsep + os.environ['PATH']

Error while installing PySWIP for use of Prolog in Python

I am trying to follow the installation guide for PySWIP in Windows, which first asks to install a 32 bit version of SWI-Prolog first and include its bin in PATH, which I did. Then I installed the 64 bit version of PySWIP since my Python-2.7 is 64 bit Anaconda version. But it doesn't work (see error below).
As suggested by some elsewhere, I also made of copy of libswipl.dll (in C:\Program Files (x86)\pl\bin ) as libpl.dll and of swipl.ico (in C:\Program Files (x86)\pl ) as pl.ico but no luck.
The error that I am getting (in Spyder and EclipsePyDev IDEs using Anaconda interpreter) when I enter the first line below is:
>>> from pyswip import Prolog
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\XXX\Anaconda\lib\site-packages\pyswip\__init__.py", line 29, in <module>
from pyswip.prolog import Prolog
File "C:\Users\XXX\Anaconda\lib\site-packages\pyswip\prolog.py", line 29, in <module>
from pyswip.core import *
File "C:\Users\XXX\Anaconda\lib\site-packages\pyswip\core.py", line 360, in <module>
_lib = CDLL(_path)
File "C:\Users\XXX\Anaconda\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application
How to fix this?
Background: I am working through the first order logic programming of natural language semantics of NLTK book, and I thought it would be nice to have a logic program that can do constraint programming (specifically, solve for equality logical constraints)
Found my answer - it was that I should have installed a 64 bit (instead of 32 bit) version of SWI-Prolog for it to work with 64 bit PySWIP and Python (even though the installation guide at PySWIP specifically mentioned 32 bit SWI-Prolog). Its working now.
PS: I didn't find the need to rename libswipl.dll or swipl.ico

Python weave blitz DLL error

I'm trying to use weave.blitz to speed up some code and I keep getting the following DLL error. If I run a simple code, e.g.
from scipy import * # or from NumPy import *
a = ones((512,512),'Float64')
b = ones((512,512),'Float64')
# now average
a[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1] \
+ b[1:-1,2:] + b[1:-1,:-2]) / 5.
from scipy import weave
expr = "a[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
weave.blitz(expr)
I get the following error:
Traceback (most recent call last):
File "C:\Users\Thijs\wtest.py", line 19, in <module>
weave.blitz(expr)
File "C:\Python27\lib\site-packages\scipy\weave\blitz_tools.py", line 65, in blitz
**kw)
File "C:\Python27\lib\site-packages\scipy\weave\inline_tools.py", line 488, in compile_function
exec 'import ' + module_name
File "<string>", line 1, in <module>
ImportError: DLL load failed: Invalid access to memory location.
I'm using the latest Pythonxy and I usually write my code in Spyder; not sure if that has anything to do with it. Any ideas?
I'm also using python 2.7 64bit/weave.inline under windows 7 and just met the same issue as you described here. I searched the whole internet but this post seems like the only related one and i got no answer.
I traced the weave.inline function and try to load the pyd from compiled binary. Then i found that the loading is successful if i try
python -c "import sys; sys.path.insert(0, 'C:\\Users\\zliu\\AppData\\Local\\Temp\\zliu\\python27_compiled'); import sc_d4c0ee9cff8db6a9b5fc8352299944210;" where the module name being some hash value apparently.
However if I start python interactive then input the exact same statements in the interactive mode, it just shows
ImportError: DLL load failed: Invalid access to memory location.
So next i tried to compare the output of python -c -v "..." and python -v, finally i was able to locate the devil different line:
import string
I have no idea why python -c and python interactive are different in this or why without this module the import show such an ambiguous message. But putting it at the beginning of the script just works for me.
I am sorry for posting to such an old thread, and I do not offer an working solution or exaplantion of the problem, it is just a comment. ImportError: DLL load failed: Invalid access to memory location. I encountered the same error when trying to make my own extension of Python programmed in C. Platform Windows 32-bit.
It was a real pain because this error appeared randomly in interactive as well as in non-interactive mode in all Python environments (Spyder, Notebook, plain console...). I compiled my code using MinGW and Python's distutils (command python setup.py install). The compilation gave no warnings or errors and produced pyd file to the correct directory. But when trying to import this module import example pro my Python code it irregularly crashed (usually only one out of five attempts to import the module succeeded).
Strange was that on another computer it worked just fine... Well, I finally found a workaround - I downloaded a newer version of MinGW (before I had used the version that comes packed in Qt SDK distribution) and compiled the module again. Then it worked with no more crashes. However I did not find any systematic solution or explanation. So I might have something to do with the compiler (maybe absence of its DLLs? I do not know exactly) that was used to generate the pyd file.

Categories

Resources