Openslide-python import error - python

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

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.

OSError: [WinError 126] when importing a library in 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.

python error ImportError: DLL load failed: The specified procedure could not be found. File "psycopg2\_psycopg.pyc", line 10, in __load

I packaged a Windows application developped in python. For the setup, no problem. When launching it up on my PC (let's say PC1), no problem. But when trying to launch it on another PC (let's say PC2), an error message shows up indicating to read the MYAPPLICATION.exe.log file. There s 1 error (only 1), here are the lines of the log :
File "MYAPPLICATION.pyc", line 9, in
File "psycopg2__init__.pyc", line 50, in
File "psycopg2_psycopg.pyc", line 12, in
File "psycopg2_psycopg.pyc", line 10, in __load
ImportError: DLL load failed: The specified procedure could not be
found.
Indications about my way of packaging :
i used the command "setup.py py2exe --includes sip" (with the proper
setup.py file) then InnoSetupCompiler.
I embedded both folders 'dist' and 'build'. I even copied the
psycopg2 folder from site-packages to 'dist' (as well as mpl-data for
matplotlib b.t.w. for which one i have no problem) before packaging
with InnoSetup, so that i'm sure the module psycopg2 is well present
on the PC2. But when I try to run it on the PC2 which occurs the
error. Indications about PC2 : it has the python modules already
installed on. (including the last version of psycog2)
So why does the psycog2 module import fails here ? Thanks.
"There are 2 DLLs that are required by pyscopg.pyd (secur32.dll & shfolder.dll) that are OS dependent." Removing them should do the trick. I got this solution from here: http://psycopg.initd.narkive.com/FX7WIxE6/import-error-using-psycopg2-with-py2exe

Pygame breaks outside of Pydev.

Me and a team of friends created this game which I am now trying to run in linux,
We developed it using python 2.7 and Pygame in windows using Aptana studio and the code fully work when running through there.
When downloading it to linux It wouldn't load saying that it could not find files. I then tried running it via CMD in windows and there is the same error.
the error so far is
Traceback (most recent call last):
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 144, in <module>
showMenu()
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 107, in showMenu
menu.init(['Start','About','Quit'], surface)
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 52, in init
self.create_strukture()
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 73, in create_strukture
self.font = pygame.font.Font(self.font_path, self.fontsize)
IOError: unable to read font filename
And the source is at:
https://github.com/ryanteck/RasPiThon/tree/master/Raspberroids/Source%20Code
Happens on both 2.7 and 2.6
Can anyone help?
Your font path data/coders_crux/coders_crux.ttf is relative.
When you start your game from another directory than your source directory, pygame can't find the font.
A simple fix is to add the following lines to the top of your script (mainmenu.py):
import os
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.path.realpath(\__file__) will get the path to your script, and with os.chdir and os.path.dirname you change the current working directory to the directory of your script.
This way, the relative paths you use will work.
PyDev sets working directory and PYTHONPATH variable for your programs. It can also set console encoding to something different than OS defaults.
Add a print self.font_path statement before creating your Font object and see if path is OK. If it's relative path, you could also use os.path.abspath (see os.path docs for details) to get better understanding of what's going on.

How to fix this dll loading python error?

I use one c++ dll in my python code.
When I run my python app on my computer, it works fine but when I copy all to another computer this happen:
Traceback (most recent call last):
File "C:\users\Public\SoundLog\Code\Código Python\SoundLog\SoundLog.py", line 9, in <module>
from Auxiliar import *
File "C:\users\Public\SoundLog\Code\Código Python\SoundLog\Auxiliar\DataCollection.py", line 4, in <module>
import SoundLogDLL
File "C:\users\Public\SoundLog\Code\Código Python\SoundLog\Auxiliar\SoundLogDLL.py", line 4, in <module>
dll = cdll.LoadLibrary(os.environ['PUBLIC'] + "\\SoundLog\\DLLs\\ForPython\\SoundLogC++WrapperDLL.dll")
File "C:\Python26\lib\ctypes\__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
File "C:\Python26\lib\ctypes\__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 14001] The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail
How can I fix this problem so that my python app work in every computer?
Note:
I only need them to work on windows 7 like mine. And I tested on another win7 computer (both 32bits).
You're using a DLL which depends on a Microsoft Visual C++ runtime which isn't installed on the target computer. You have a few options:
Install or copy the Visual C++ runtime libraries to the target computer. Installation is done by adding merge modules to your installer (if you have one) or by running the redistributable installer (vcredist.exe). The various methods are documented here for VS2005 (other versions will be similar).
If you built the DLL, you can change the project settings to statically link the runtime libraries. See this answer to see how this is done.

Categories