Unable to load python27.dll with winforms but console application works


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, 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, 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.

asked on Stack Overflow Jul 12, 2012 by Bicubic

2 Answers


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.

answered on Stack Overflow Jul 12, 2012 by Bicubic
  1. I added reference to python.runtime.dll (which is located in xxx\Python36\Lib\site-packages) to solve the dll loading problem.
  2. 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.
  3. I moved my 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!

answered on Stack Overflow Apr 8, 2019 by rml

User contributions licensed under CC BY-SA 3.0