OSError: [WinError 126] when importing a library in python - python

i am using the cubemos skeleton tracking Api (using for extract skeleton keypoints from pictures and videos).
i have a very weird problem:
everything was working fine, until one day, i received this error (also added image):
Traceback (most recent call last):
File "C:/Users/omria/PycharmProjects/skeletonTracking/skeletonTracking.py", line 3, in <module>
from cubemos.skeleton_tracking.nativewrapper import Api, SkeletonKeypoints
File "C:\Users\omria\PycharmProjects\skeletonTracking\venv\lib\site-packages\cubemos\skeleton_tracking\__init__.py", line 1, in <module>
from .nativewrapper import *
File "C:\Users\omria\PycharmProjects\skeletonTracking\venv\lib\site-packages\cubemos\skeleton_tracking\nativewrapper.py", line 28, in <module>
os.path.join(cubemos_dir, "bin", "cubemos_skeleton_tracking.dll")
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\ctypes\__init__.py", line 364, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
enter image description here
and line 3 is when i'm importing the library:
from cubemos.skeleton_tracking.nativewrapper import Api
another weird thing, is that the problem was fixed randomly without me doing anything, and then it returned again!
i tried reinstalling the cubemos SDK, rebooting the computer, debug, and i really don't know what is the source of this problem.
it seems like its some problem with my OS (i have windows 10, and using python 3.7 and VENV and PIP for the project)
i tried to figure out if the path is the problem, but again, it was working before, and i can't really tell if that is the problem. what do you think is causing this?
i will appreciate any help.
thanks !

I had the same problem using a conda env. Demos worked but not the python sample. I explicitly added %CUBEMOS_SKEL_SDK%\bin to my Path environment since I had nothing before. That got it going. The %CUBEMOS_SKEL_SDK% was set up okay from the start.

Related

Import dll using ctypes in virtual environment

As per title I am using ctypes to access a .dll file from python. Everything works fine if I run the following code from my normal shell:
import ctypes as ct
import os
path = '\\some_path\\file.dll'
print(os.path.isfile(path))
lib = ct.CDLL(path)
Output:
True
However if I try to run it from a virtual environment the same code gives me the output + error:
True
Traceback (most recent call last):
File ".\Test-c.py", line 4, in <module>
lib = ct.CDLL(path)
File "C:\Users\au684834\Miniconda3\envs\development\lib\ctypes\__init__.py", line 351, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
Reading around I couldn't find this specific problem, does anyone know a simple solution? Note that the file is found so the path is likely not the problem.
I have to run this inside a virtual environment because this is an addition to a larger project.
Many thanks
EDIT: modified the path to avoid confusion with the Windows backslash problem. I have also added os.path.isfile() to show that the file exists and it is found.
So, it turns out the problem was not ctypes, permissions or anything like that: the problem was the virtual environment. in fact there was a problem, not only with my environment, but with the miniconda installation itself. I was not able to reinstall the environment nor to update miniconda. I solved everything by reinstalling miniconda from scratch and recreating the environment. Now the code above works as expected.

Openslide-python import error

I receive the following error when running import openslide from inside python terminal
<code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\dev_res\python\python2_713\lib\site-packages\openslide\__init__.py", line 29, in <module>
from openslide import lowlevel
File "C:\dev_res\python\python2_713\lib\site-packages\openslide\lowlevel.py", line 41, in <module>
_lib = cdll.LoadLibrary('libopenslide-0.dll')
File "C:\dev_res\python\python2_713\lib\ctypes\__init__.py", line 440, in LoadLibrary
return self._dlltype(name)
File "C:\dev_res\python\python2_713\lib\ctypes\__init__.py", line 362, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 127] The specified procedure could not be found
</code>
My OS is Windows 64-bit and I am using Python 2.7.13 (64-bit). I installed the OpenSlide binaries (2016-7-17 64-bit release) and added the corresponding bin folder to my system path. I then installed python-openslide using pip. Please note that this error is different from WindowsError: [Error 126] The specified module could not be found (see question) which occurs when the windows binaries have not been added to the system path.
Same problem occurs when using Python 3.5.3. Interestingly, I tried the same workflow except with the 32-bit versions (python 2.7 32-bit and 32-bit openslide binaries) and I did not receive this error. However, I would prefer to use the 64-bit versions.
Any help would be greatly appreciated. Thanks!
After receiving help from the openslide-python authors on github, I was able to get a working solution.
The problem is that there are multiple dll's in your search path with the same name as those required by openslide. In my case for example, zlib1.dll is not only found in the openslide\bin directory but also in a MATLAB directory, github directory, and an Intel wifi directory. When python asks the operating system to find the required dll, the operating system is going to return the first name-matching instance that it encounters which might not be the openslide\bin one.
A quick fix is to start python from inside the openslide\bin directory. In other words, start a command prompt, navigate to the openslide\bin directory, type "python" and now typing import openslide should work fine. This works because the directory from which python was started is searched first for matching dll's. A more rigorous solution that will prevent you from having to start the terminal every time from inside openslide\bin is to add the following to the beginning of lowlevel.py file (which can be found in Lib\site-packages\openslide directory of your python installation)
os.environ['PATH'] = "path-to-openslide-bin" + ";" + os.environ['PATH']
Note: Replace path-to-openslide-bin with the correct path
Every time you type import openslide lowlevel.py is run which tries to load the appropriate dll's. The above line of code adds the location of the dll's to the beginning of the environment path which ensures that this folder is at the top of the search hierarchy and will therefore be found before the other name-matching instances.
You can view the corresponding issue/user report on github here

Installing Gumbo and its Python wrapper

I am trying to use Gumbo's python wrapper to parse HTML.
My operating system is Ubuntu 14.04.3 LTS. I am using Python2.7
I have cloned the latest version of Gumbo from github.
I followed the installation steps provided on Github.
The installation of the libraries, both the C library and the python wrapper, appeared to be successful ( No error message, both printed successful messages at the end )
C library final message:
Libraries have been installed in: /usr/local/lib
Python wrapper message:
Installed
/usr/local/lib/python2.7/dist-packages/gumbo-0.10.1-py2.7.egg
Processing dependencies for gumbo==0.10.1 Finished processing
dependencies for gumbo==0.10.1
The first problem I encountered was when I tried to open pydoc for gumbo, to better understand the library.
pydoc gumbo produced the following error:
problem in gumbo - <type 'exceptions.OSError'>: /usr/local/lib/python2.7/dist-packages/gumbo-0.10.1-py2.7.egg/gumbo/libgumbo.so: cannot open shared object file: No such file or directory
Searching for the message yielded a single result.
It was not of much use to me.
Looking at the dist-packages directory, I noticed that libgumbo.so was not in /usr/local/lib/python2.7/dist-packages/gumbo-0.10.1-py2.7.egg/gumbo/ . All other files ( soup-adapter.py, gumboc.py, etc ) where there however.
The installation of the C library placed libgumbo.so ( and some other libraries, like libgumbo.a libgumbo.la, etc ) in /usr/local/lib. So, as a work around, I created a simlink from .../dist-packages/gumbo-0.10.1-py2.7.egg/gumbo/ to /usr/local/lib.
This got pydoc gumbo to work.
I tried to import gumbo and soup-adapter in the interpreter after. I received the following error:
import soup_adapter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "soup_adapter.py", line 26, in <module>
import gumboc
File "gumboc.py", line 44, in <module>
os.path.dirname(__file__), _name_of_lib))
File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libgumbo.so: cannot open shared object file: No such file or directory
I am not sure how to proceed or how exactly to get gumbo to work.

"OSError: dlopen(libSystem.dylib, 6): image not found" (OS X + macports + Celery 3.1.7)

I just updated celery via pip (1.5) to the latest version (3.1.7), but I get a fatal exception which I don't understand as soon I try to import the library. By running:
from celery import Celery in the shell I get:
File "<console>", line 1, in <module>
File "/Users/davidezanotti/CygoraPythonEnv/lib/python2.7/site-packages/celery/__init__.py", line 130, in <module>
from .five import recreate_module
File "/Users/davidezanotti/CygoraPythonEnv/lib/python2.7/site-packages/celery/five.py", line 51, in <module>
from kombu.five import monotonic
File "/Users/davidezanotti/CygoraPythonEnv/lib/python2.7/site-packages/kombu/five.py", line 47, in <module>
libSystem = ctypes.CDLL('libSystem.dylib')
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: dlopen(libSystem.dylib, 6): image not found
I'm using python 2.7.6 with VirtualEnv and macports on OS X 10.8.5
I understand that the problem is that an underlying dependency ("libSystem.dylib") is missing, but I don't know how to fix this issue
(under /usr/lib/ I can find libSystem.dylib on my machine)
Just add /usr/lib to DYLD_FALLBACK_LIBRARY_PATH will resolve this issue.
The problem was that I had defined a DYLD_FALLBACK_LIBRARY_PATH in my .bash_profile, by following literally the notes about macports in Django documentation on postgis: https://docs.djangoproject.com/en/1.6/ref/contrib/gis/install/#macports
but this leads to unexpected behavior like the one I faced, anyway by commenting it out, all seems to work!

Python: PIL - [Errno 32] Broken pipe when saving .png

What I'm trying to do here is save the contents of a Tkinter Canvas as a .png image using PIL.
This is my save function ('graph' is the canvas).
def SaveAs():
filename = tkFileDialog.asksaveasfilename(initialfile="Untitled Graph", parent=master)
graph.postscript(file=filename+".eps")
img = Image.open(filename+".eps")
img.save(filename+".png", "png")
But it's getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Adam\Desktop\Graphing Calculator\Graphing Calculator.py", line 352, in SaveAs
img.save(filename+".png", "png")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1406, in save
self.load()
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 283, in load
self.im = Ghostscript(self.tile, self.size, self.fp)
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 72, in Ghostscript
gs.write(s)
IOError: [Errno 32] Broken pipe
I'm running this on Windows 7, Python 2.7.1.
How do I make this work?
oh I just get the same error. I have solve it now
just do the following after installing PIL and Ghostscript
1) Open C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py
2) Change code near line 50 so that it looks like this:
Build ghostscript command
command = ["gswin32c",
"-q", # quite mode
"-g%dx%d" % size, # set output geometry (pixels)
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % file,# output file
"-"
]
Make sure that gswin32c.exe is in the PATH
good luck
It looks like the Ghostscript executable is erroring out and then closing the connection. Others have had this same problem on different OSes.
So, first I would recommend that you confirm that PIL is installed correctly--see the FAQ page for hints. Next, ensure that Ghostscript is installed and working. Lastly, ensure that Python can find Ghostscript, for example by running a PIL script that works elsewhere.
Oh, also--here are some tips on catching the broken pipe error so your program can be more resilient, recognize the problem, and warn the end-user. Hope that helps!
I have realized that while Python 2.7 has this EPEImagePulgin.py, Anaconda also has it. And unfortunately Anaconda's file is an older version. And unfortunately again, when you run your from Spyder environment it was picking up the epsimageplugin.py file from anaconda folder.
So I was getting similar broken pipe error.
When I went into python 2.7 directory and opened python console and then ran my code, it ran just fine.
Because the lates epsimageplugin.py file takes into consideration the windows environment and appropriate ghostscript exe files. Hope this helps.

Categories