ImportError: DLL load failed: %1 is not a valid Win32 application Only When Outside Python27 Directory

2.1k views Asked by At

I have an odd variation of the common "ImportError: DLL load failed: %1 is not a valid Win32 application" error. I only get this error when I try to import a 3rd party library while run a python script outside of the python27 directory. For instance, if I do "import numpy" while inside python27, it works fine, but if I try to import numpy while in any other directory, I get the above error. Essentially I can run "python" in any directory, but can only import 3rd party libraries if I run it from the python27 directory. If anyone has any ideas as to why this would be, I'd be very appreciative. Here's some information about my system paths.

Applicable Windows System Paths:

PYTHONPATH = C:\Python27\Lib

PYTHONHOME = C:\Python27

sys.path is equal to:

['', 'C:\Python27\Lib', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\libtk', 'C:\Python27', 'C:\Python27\lib\site-packages', ' C:\Python27\lib\site-packages\win32', 'C:\Python27\lib\site-packages\win32\lib', 'C:\Python27\lib\site-packages\Pythonwin']

And if I run win_add2path.py I get: No path was added

PATH is now: C:\Users\Mike\AppData\Local\Enthought\Canopy\User;C:\Users\Mike\AppData\Local\Enthought\Canopy\User\Scripts;C:\Python27;C:\Python27\Scripts

Expanded: C:\Users\Mike\AppData\Local\Enthought\Canopy\User;C:\Users\Mike\AppData\Local\Enthought\Canopy\User\Scripts;C:\Python27;C:\Python27\Scripts

Part of me feels that the Enthought Canopy path is screwing it up (that directory no longer exists), but the Python27 path is also there so it shouldn't be an issue...

EDIT: I believe I now know what is causing the problem, but not how to fix it. So apparently there was a python.exe in the enthought canopy folder, and this is the one my comptuer was using, not the one in python27 (which is weird because I uninstalled enthought canopy). However, my computer now can't find the python.exe in python27 even though that directory is added to my system path... It gives me the old "python is not recognized as an internal or external command" shindig.

Edit Well, I restarted the command prompt and now it works... I guess the removal of the enthought canopy path variable hadn't taken affect yet.

2

There are 2 answers

0
Ulrich Eckhardt On

The Python modules are either plain Python scripts (I'll ignore those here) or they are DLLs, even if their filename doesn't indicate that they are. What you can do is to run dependencywalker on such a module to find out which other DLLs it depends on. If the according DLL is not in the normal system locations, MS Windows will search for it in the current working directory, which explains the differences you encounter. For further information, see the documentation of the win32 function LoadLibrary() and related functions for information on how DLL paths are resolved, which will also tell you the options you can tweak to help the system find the dependent DLLs.

0
Msquared On

So for reference the source of the issue was residue from an old Enthought Canopy installation. The computer was using that installation of python (which didn't have the 3rd party libraries installed) instead of the one in Python27. I deleted that install from the system path and restarted the command prompt and now all is well.