python.net: Unable to load python27.dll with winforms but console application works
-
20-06-2021 - |
Question
I'm using .net framework 4.5 but have had the same result with 4.0:
If I write a bare bones console application to initialize python.net, it works as expected. If I try to do the same thing from a winforms application, I get "Unable to load DLL 'python27': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
If I reference the functioning console application from the winforms application and call a method that initializes python.net, the same thing happens. Build settings are default for both projects.
I even went as far as putting python27 straight into the executable folder but the same error still occurs so I'm guessing this might be a 32 vs 64 bit issue even though both projects are configured for any cpu.
La solution
After some more poking around I found out two things:
Changing the build target to x86 solved the missing DLL error, however the application still cryptically crashed when calling PythonEngine.Initialize(). I was able to solve this by making sure that the initialization takes place before any other code is executed.
Autres conseils
- I added reference to python.runtime.dll (which is located in xxx\Python36\Lib\site-packages) to solve the dll loading problem.
- I changed the build target to x64 solved my PythonEngine.Initialize() problem. And @Bicubic solved his problem by changing to x86. So you just can not let the c# target to be AnyCPU. Try both.
- I moved my xxx.py to the xxx\Python36 folder to solve the py.import("xxx") error.
I can have some fun now :) Hope you guys good luck and have fun!