Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 875
user3763012 Avatar asked Feb 04 '23 10:02

user3763012


1 Answers

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

like image 50
user3763012 Avatar answered Feb 16 '23 03:02

user3763012