WindowsError: [Error 126] The specified module could not be found - python

I am loading a dll in python using following code:
if os.path.exists(dll_path):
my_dll = ctypes.cdll.LoadLibrary(dll_path)
But I am continuously getting the following error
WindowsError: [Error 126] The specified module could not be found
dll is present at the specified path, but I didn't understand why I'm getting the error.

Note that even if the DLL is in your path. If that DLL relies on other DLLs that are NOT in your path, you can get the same error. Windows could not find a dependency in this case. Windows is not real good at telling you what it could not find, only that it did not find something. It is up to you to figure that out. The Windows dll search path can be found here:
http://msdn.microsoft.com/en-us/library/7d83bc18.aspx
In my case, being sure all needed dlls were in the same directory and doing a os.chdir() to that directory solved the problem.

When I see things like this - it is usually because there are backslashes in the path which get converted.
For example - the following will fail - because \t in the string is converted to TAB character.
>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:\tools\depends\depends.dll")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\tools\python271\lib\ctypes\__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
File "c:\tools\python271\lib\ctypes\__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found
There are 3 solutions (if that is the problem)
a) Use double slashes...
>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:\\tools\\depends\\depends.dll")
b) use forward slashes
>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:/tools/depends/depends.dll")
c) use RAW strings (prefacing the string with r
>>> import ctypes
>>> ctypes.windll.LoadLibrary(r"c:\tools\depends\depends.dll")
While this third one works - I have gotten the impression from time to time that it is not considered 'correct' because RAW strings were meant for regular expressions. I have been using it for paths on Windows in Python for years without problem :) )

On the off chance anyone else ever runs into this extremely specific issue..
Something inside PyTorch breaks DLL loading. Once you run import torch, any further DLL loads will fail. So if you're using PyTorch and loading your own DLLs you'll have to rearrange your code to import all DLLs first. Confirmed w/ PyTorch 1.5.0 on Python 3.7

I met the same problem in Win10 32bit OS. I resolved the problem by changing the DLL from debug to release version.
I think it is because the debug version DLL depends on other DLL, and the release version did not.

If you are using GCC to compile it for Windows, it's possible that the error is because dependent libraries can't be found.
Using the -static flag if linking with GCC might fix that.

Also this could be that you have forgotten to set your working directory in eclipse to be the correct local for the application to run in.

In Windows, it's possible. You will need to install: Visual C++ Redistributable for Visual Studio 2015. I had the same problem and I installed both version (Windows x86 and Windows x64). Apparently both are necessary to make it work.

Tried to specify dll path in different ways (proposed by #markm), but nothing has worked for me.
Fixed the problem by copying dll into script folder. It's not a good solution, but ok for my purposes.

for me install Microsoft Visual C++ 2015 Redistributable Update 3 from https://www.microsoft.com/en-us/download/details.aspx?id=53587 solved it.

if you come across this error when you try running PyTorch related libraries you may have to consider installing PyTorch with CPU only version i.e. if you don't have Nvidia GPU in your system.
Pytorch with CUDA worked in Nvidia installed systems but not in others.

There is a promising answer at Problem updating bokeh: [WinError 126] The specified module could not be found.
It hints at https://github.com/conda/conda/issues/9313.
There, you find:
It's a library load issue. More details at
github.com/conda/conda/issues/8836 You probably have a broken conda
right now. You can use a standalone conda from
repo.anaconda.com/pkgs/misc/conda-execs to repair it:
standalone-conda.exe update -p C:\ProgramData\Anaconda3
conda-package-handling You should get version 1.6.0, and the problems
should go away.
Thus, it might simply be a conda issue. Reinstalling standalone conda might repair the error. Please comment whoever can confirm this.

problem solved for me.
I changed version from pytorch=1.5.1 to pytorch=1.4 and typed the below command in anaconda prompt window
conda install pytorch==1.4.0 torchvision==0.5.0 -c pytorch

NestedCaveats solution worked for me.
Imported my .dll files before importing torch and gpytorch, and all went smoothly.
So I just want to add that its not just importing pytorch but I can confirm that torch and gpytorch have this issue as well. I'd assume it covers any other torch-related libraries.

This is probably because a runtime dependency of one of the DLLs was not found on your system. I think that the expected Microsoft Visual C runtime DLL is missing from your system.
Install this:
https://www.microsoft.com/en-US/download/details.aspx?id=40784

I fixed this issue by installing VC redistributable for Visual Studio 2012 and the up-to-date Visual Studio versions. After reboot, the problem has gone.

Related

How to compile TeX file from Python

I'm trying to create a pdf file inside a python/django project. I'm using PyLatex, which helps, but it's generate_pdf method breaks when it calls pdflatex. I keep getting this error:
FileNotFoundError: [Errno 2] No such file or directory: 'pdflatex'
I tried everything - os.system() ... subprocess. Any way I try, it can't find pdflatex. I'm running on Windows, but I need to be able to to do this within the web-app anyway.
Thanks for any help!
You just need to install pdflatex (and you will need to install on your server when you release your web-app). Instructions for installing on windows can be found in the top answer here.
Do you have "pdflatex" installed? As per this question, you may have to install "MiKTeX" and point os.system() at C:\Program Files\MiKTeX <MikTeX version>\miktex\bin to make it work.
Hope that helps.
Do you have texlive installed? If yes, then the issue is with the system path variable. If not already added, add it to the system path.
#
Updated answer:
I took the time and decided to replicate your issue. I too use windows.
I copied the basic example from Pylatex and tested it on my machine, I was able to replicate the same error as you despite having TexLive installed. To solve it I did the following steps:
Installed MikTex (I believe you have it installed).
Tried running it again but failed with the error missing package latexmk.
Using MikTex package manager, I installed the latexmk package.
Tried running but still failed, error missing perl.exe command.
I had to install perl to my windows, use the following links -- https://learn.perl.org/installing/windows.html
When I tried again, it compiled successfully.
The issue was not having the package latexmk for MikTex and perl.exe of which you should have posted as part of the error logs. Try that and let me know if successful.

LHAPDF: undefined symbol when running in Python

I'm trying to use the Python interface for LHAPDF, but I receive the following error message:
Traceback (most recent call last):
File "test.py", line 2, in <module>
import lhapdf
ImportError: /home/n17182559/LHAPDF/lib/python2.7/site-packages/lhapdf.so: undefined symbol: _ZN6LHAPDF6ConfigD1Ev
I'm running on Ubuntu 17.04, using Python 2.7, have Boost installed and have a working C++ compiler (g++). I believe I have successfully installed LHAPDF, as I followed the instructions on their website and got no error message (only warnings that auto_ptr is deprecated, but I don't think I have control over that). I did add the ~/LHAPDF/lib/python2.7/sitepackages/ directory to $PYTHONPATH and ~/LHAPDF/bin/ directory to $PATH. I am using LHAPDF 6.1.6 (latest version as of writing this).
I get this error message from a test.py file that merely contains
#!/usr/bin/python
import lhapdf
If it can help you help me, I have found someone with a similar problem, but the cause seems not to be the same thing (I don't have Anaconda installed). As they solved their problem by removing Anaconda from $PATH, here are my $PATH and $PYTHONPATH as they might be the source of the problem (although I don't see how):
$PATH
/home/n17182559/LHAPDF/bin:/opt/applications/geant4/geant4.10.02-install/bin:/home/n17182559/ROOT/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:
$PYTHONPATH
/home/n17182559/ROOT/lib:/home/n17182559/LHAPDF/lib/python2.7/site-packages:
Hope you guys can help!
I solved it (with external help), if anyone wants the solution. The problem was that the main LHAPDF lib directory was not linked to my LD_LIBRARY_PATH. This command line solved it:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/LHAPDF/lib/libLHAP‌​DF.so
(which I added to my ~/.bashrc file).
I can't speak to your specific problem, but when I get this class of error, it stems from a version mismatch between packages: lhapdf depends on another package to define that arcane "semi-hidden" symbol, but the package on which it depends is of a version different from the one expected; the older/newer version doesn't provide that symbol.
It is a version mismatch (of Python), which is likely caused by upgrading Python after LHAPDF is installed.
Therefore you may have to reinstall LHAPDF, or you could install a second LHAPDF if you use two versions of Python (as of now I am not aware of a solution other than this).
Let us assume you use LHAPDF of version 6.2 or higher, which is much simpler. First you would like to set the path in your shell profile so that it points to the version of Python you want to install LHAPDF for. For bash, you could do
export PATH=path/to/python
then you would like to follow the instruction and do
tar xf LHAPDF-6.X.Y.tar.gz
cd LHAPDF-6.X.Y
./configure --prefix=/path/for/installation
make
make install
After the installation is done, you can add LHAPDF to the path by
export PYTHONPATH=path/to/lhapdf/lib/pythonx.y/site-packages/
export LD_LIBRARY_PATH=path/to/lhapdf/lib
export PATH=path/to/lhapdf/bin/:$PATH
export LHAPDF_DATA_PATH=path/to/lhapdf/share/LHAPDF

Installing OpenCV - DLL load failed: %1 is not a valid Win32 application [duplicate]

I have a situation very much like the one at Error "ImportError: DLL load failed: %1 is not a valid Win32 application", but the answer there isn't working for me.
My Python code says:
import cv2
But that line throws the error shown in the title of this question.
I have OpenCV installed in C:\lib\opencv on this 64-bit machine. I'm using 64-bit Python.
My PYTHONPATH variable: PYTHONPATH=C:\lib\opencv\build\python\2.7. This folder contains cv2.pyd and that's all.
My PATH variable: Path=%OPENCV_DIR%\bin;... This folder contains 39 DLL files such as opencv_core246d.dll.
OPENCV_DIR has this value: OPENCV_DIR=C:\lib\opencv\build\x64\vc11.
The solution at Error "ImportError: DLL load failed: %1 is not a valid Win32 application" says to add "the new opencv binaries path (C:\opencv\build\bin\Release) to the Windows PATH environment variable". But as shown above, I already have the OpenCV binaries folder (C:\lib\opencv\build\x64\vc11\bin) in my PATH. And my OpenCV installation doesn't have any Release folders (except for an empty one under build/java).
What's going wrong? Can I tell Python to verbosely trace the loading process? Exactly what DLL files is it looking for?
I noticed that, according to http://www.dependencywalker.com/, the cv2.pyd in C:\lib\opencv\build\python\2.7 is 32-bit, whereas the machine and the Python I'm running are 64-bit. Could that be the problem? And if so, where can I find a 64-bit version of cv2.pyd?
Unofficial Windows Binaries for Python Extension Packages
You can find any Python libraries from here.
Please check if the Python version you are using is also 64 bit. If not then that could be the issue. You would be using a 32-bit Python version and would have installed a 64 bit binaries for the OpenCV library.
Wow, I found yet another case for this problem. None of the above worked. Eventually I used python's ability to introspect what was being loaded. For Python 2.7, this means:
import imp
imp.find_module("cv2")
This turned up a completely unexpected "cv2.pyd" file in an Anaconda DLL directory that wasn't touched by multiple uninstall/install attempts. Python was looking there first and not finding my good installation. I deleted that cv2.pyd file and tried imp.find_module("cv2") again and python immediately found the right file and cv2 started working.
So if none of the other solutions work for you, make sure you use Python introspection to see what file Python is trying to load.
In my case, I have 64-bit Python, and it was lxml that was the wrong version--I should have been using the x64 version of that as well. I solved this by downloading the 64-bit version of lxml here:
https://pypi.python.org/pypi/lxml/3.4.1
lxml-3.4.1.win-amd64-py2.7.exe
This was the simplest answer to a frustrating issue.
I just had this problem. It turns out it was just because I was using an 64-bit version of the OpenCV file. I tried the x86 and it worked.
I had the same problem. Here's what I did:
I downloaded the pywin32 wheel file from here, then
I uninstalled the pywin32 module. To uninstall, execute the following command in a command prompt.
pip uninstall pywin32
Then, I reinstalled pywin32. To install it, open the command prompt in the same directory where the pywin32 wheel file lies. Then execute the following command.
pip install <Name of the wheel file with extension>
Wheel file will be like: piwin32-XXX-cpXX-none-win32.whl
It solves the problem for me.
I copied cv2.pyd file from /opencv/build/python/2.7/x86 folder instead of from /x64 folder to C:/Python27/Lib/site-packeges. I followed rest of the instructions provided here.
Added by someone else, not verified: I also copy file cv2.pyd to folder C:/Python27/Lib/site-packages/cv2. It works.
For me the problem was that I was using different versions of Python in the same Eclipse project. My setup was not consistent with the Project Properties and the Run Configuration Python versions.
In menu Project → Properties → PyDev, I had the Interpreter set to Python 2.7.11.
In Run Configurations → Interpreter, I was using the Default Interpreter. Changing it to Python 2.7.11 fixed the problem.
If your build system (CMake in my case) copies the file from <name>.dll to <name>.pyd, you will get this error if the original file wasn't actually a DLL file. In my case, building shared libraries got switched off, so the underlying file was actually a *.lib.
I discovered this error by loading the pyd file in Dependency Walker and finding that it wasn't valid.
Update NumPy.
pip install numpy --upgrade
It works for me!
This one worked for me:
pip install -- pywin32==227
I faced the same issue when I uninstalled and reinstalled a different version of 2.7.x of Python on my system using a 32-bit Windows Installer. I got the same error on most of my import statements.
I uninstalled the newly installed Python, downloaded a 64-bit Windows installer, reinstalled Python again, and it worked.
So I had problems installing vtk under Windows (as I use Python 3.7, there isn't any binary available so far. Just for older Python versions pip install vtk is not working)
I did wrote Python in my cmd:
Python 3.7.3 on win32
So I now know I have Python 3.7.3 running on a 32 bit.
I then downloaded the correct wheel at VTK‑8.2.0‑cp37‑cp37m‑win32.whl
Next I installed that wheel:
pip install VTK-8.2.0-cp37-cp37m-win32.whl
Then I tested it and it worked:
python
import vtk
I experienced the same problem while trying to write code concerning speech-to-text.
The solution was very simple. Uninstall the previous pywin32 using the pip method:
pip uninstall pywin32
The above will remove the existing one which is by default for 32 bit computers. And install it again using
pip install pywin32
This will install the one for the 64 bit computer which you are using.
I had a similar issue while trying to run uvicorn,
Creating a new virtual environment and reinstalling the python packages worked
You can install opencv from official or unofficial sites.
Refer to this question and this issue if you are using Anaconda.
It has a very simple solution.
After installing opencv
place
cv2.pyd from C:\opencv\build\python\2.7\ **x64** to C:\Python27\Lib\site-packages
instead of, place cv2.pyd from C:\opencv\build\python\2.7\ **x86** to C:\Python27\Lib\site-packages
I got this error when trying to import MySQLdb.
What worked for me was to uninstall Python and then reinstall it.
I got the error after installing npm (https://www.npmjs.com/get-npm). One thing it did was install Python even though I already had it.
First I copied cv2.pyd from /opencv/build/python/2.7/x86 to C:/Python27/Lib/site-packeges. The error was
"RuntimeError: module compiled against API version 9 but this version of numpy is 7"
Then I installed numpy-1.8.0-win32-superpack-python2.7.exe and OpenCV works fine.
>>> import cv2
>>> print cv2.__version__
2.4.13
Please make sure that you have installed a Python 2.7.12 or below version. Otherwise you will definitely get this error.
Make sure the Oracle client is 64 bit installed if the OS is 64 bit.
Make sure the Microsoft Visual C++ compiler for Python 2.7 is 64 for bit for a 64 bit OS or 32 bit for 32 bit.
Note: If your OS is 64 bit, install all packages of 64 bit or if the OS is 32 bit, install the 32-bit package.
This has worked for me. I have tried different methods, but this was my best solution.
Open a command prompt and type the following;
pip install opencv-python
(Make sure your Internet connection is on.)
After that, try importing it again.
It could also be that your Anaconda version is 32 bit when it should be 64 bit.
If you are using pycharm I go to settings -> python interpretation and click the + button and search for the name on the list of python packages there
An image showing where to go when you want to install something
I found the solution. Maybe you can try to use the cmd window rather than the Anaconda prompt window to start your first Scrapy test.

ImportError: DLL load failed: Invalid access to memory location. Using aubio in Python

For a schoolproject I need to make use of the aubio library. However, I have a problem compiling it for Windows.
I downloaded the latest source from his git (0.4.0 alpha). Then I compiled it using Cygwin, using the --with-target-platform=win32 to cross-compile it for Windows. It uses waf by the way.
This works without any errors.
Next step is copying the compiled file (libaubio.dll.a) to MinGW library folder. Then I want to compile the Python wrapper for the module, but it shows up the ld.exe cannot find -laubio error. Renaming the libaubio.dll.a to libaubio.a resolves this and compiling succeeds succesfully.
Installing it into the Python folder works perfectly too. But here starts the problem. When trying to import aubio, I get this error: ImportError: DLL load failed: Invalid access to memory location.
I have no clue on how to solve this problem. Can anybody help? Or explain the error to me?
Thanks in advance!
Xander
PS. It compiles perfectly on both OSX and Ubuntu.
Well, this may not be the right solution for you, just a hint. 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 32bits.
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, finally I found 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.

Installing mapnik on Windows XP fails with the message "ImportError: DLL load failed: The specified procedure could not be found."

I'm trying to install mapnik on Windows XP.
After diligently following the instructions on their website (http://trac.mapnik.org/wiki/WindowsInstallation), it fails with the message:
File "<stdin>", line 1, in <module>
File "C:\mapnik-0.7.1\python\2.6\site-packages\mapnik\__init__.py", line 43, in <module> from _mapnik import *
ImportError: DLL load failed: The specified procedure could not be found.
I tried the OSGEO4W installer as well, which is no more successful but before displaying the same error message also produces a pop-up saying:
"The procedure entry point xmlCtxtReadMemory could not be located in the dynamic link library libxml2.dll."
The Trouble Shooting suggestions highlighted a couple of possible missing dlls. I installed both of those but failed to resolve the problem.
Dependency Walker identified a further three missing dlls, which I also installed, and yet still without solving the problem.
Any suggestions hugely appreciated!
This is quite an old thread. However I just ran into the same problem and was able to solve it with different hints which I found across the web.
I had to make sure that I have the 32bit version of Python. I used Windows x86 MSI Installer (2.7.2)
I set the PATH variable for Python: C:\Program Files (x86)\Python27
I downloaded and installed Mapnik v2.2.0 Windows 32 bit Package (so no SDK version)
I set the PATH variables for the lib and the bin folder in this order: C:\Program Files\mapnik-v2.2.0\lib;C:\Program Files\mapnik-v2.2.0\bin (here comes the important part: make sure you put these two entries at the very beginning of the path variable as stated by zvolsky at https://github.com/mapnik/mapnik-packaging/issues/109)
Make sure you have the libxml2.dll in your c:\windows folder as mentioned by Joan Natalie at Installing Mapnik 2.2.0 in windows 7 with Python 2.7 I just copied the dll file from my mapnik install folder C:\Program Files\mapnik-v2.2.0\lib
After that I was able to execute the command "import mapnik" without any issues. I was also able to execute the python demo as stated here at number 5: https://gist.github.com/springmeyer/5651701
So I guess I had two problems. First the fact that I was somehow missing the libxml2.dll file and second that because I put the mapnik entries in the PATH variable at the end, python somehow took a wrong one since there are many of the same dll's on my system.
Hope I could provide the missing solution here.
Try installing libxml / libxml2 for windows. Seems you might be missing that as a dependancy or yourl libxml2.dll may be out of date. zlatkovic.com is the place for windows binaries of libxml:
libxml(2)
Downloads here: ftp://ftp.zlatkovic.com/libxml/
I had the same problem (on windows server 2012 R2).
i searched a lot on google and i tried all of the above answers.
btw today mapnik is fixed the libxml2.dll by linking with static address.
And Finally my problem was using double quotation in first and last of the mapnik lib path("C:\mapnik-v2.2.0\lib") in PATH variable!!!!
you should NOT use double quotation in system environment variables.
i changed "C:\mapnik-v2.2.0\lib" to C:\mapnik-v2.2.0\lib and every thing works fine. ;)

Categories