PythonEngine.Initialize() fails without any error message - c#

I am trying to embed Python in a C#-application but I cannot seem to get it to work.
My code is
using System;
using Python.Runtime;
namespace NLP.Python
{
public class PythonRumtime
{
public PythonRuntime()
{
//Location of python installation
PythonEngine.PythonHome = "C:\\Python27";
PythonEngine.Initialize();
}
}
}
When I try to create an instance of this class, the code fails without any errors when invoking PythonEngine.Initialize()
I have already added a reference to the dll-file Python.Runtime that was installed with pythonnet.
I have no idea what else to do.
I have installed pythonnet-2.1.0 via pip on a Windows 64-bit machine with a 32-bit installation of Python 2.7.11.
EDIT It appears that the error occurs at at call to Py_Initialize() in Python.Runtime.dll.
According to https://docs.python.org/2/c-api/init.html "There is no return value; it is a fatal error if the initialization fails."

This is a bug in python.org official windows installer for python 2.7.11 32-bit:
https://bugs.python.org/issue25824
Try Anaconda, maybe WinPython or python 2.7.10.

Related

C# Class Library throws FileNotFoundException while trying to load another library

I try to connect to a fiscal device with a C#.
I use this documentation to do so: http://integration.atol.ru/api-en/#connection-to-project
So basically I have a driver of the device installed on my PC (fprt10.dll) and there is a "wrapper" assembly that allows me to work with this driver from C# (Atol.Drivers10.Fptr.dll). I import this wrapper into my project as a reference.
I have the following constructor in my class:
public MyClass()
{
IFptr fiscalPrinter = new Fptr();
// Here comes several settings to configure connection
fiscalPrinter.applySingleSettings();
fiscalPrinter.open();
fiscalPrinter.beep();
fiscalPrinter.close();
}
To test the solution I use another application, that loads my Class Library as a dependency.
When I call a constructor of MyClass I get an exception:
System.IO.FileNotFoundException: Driver not installed
at Atol.Drivers10.Fptr.Fptr.loadDriver(String path)
at Atol.Drivers10.Fptr.Fptr..ctor()
at MySolution.MyClass.MyClass()
...
If I create instance of Fptr with a path to the driver
IFptr fiscalPrinter = new Fptr(#"C:\path\fptr10.dll")
I get the slightly different exception, but I believe the problem is the same:
System.IO.FileNotFoundException: Can`t load driver library "C:\path\fptr10.dll"
at Atol.Drivers10.Fptr.Fptr.raiseNotFoundError(String path, Exception reason)
at Atol.Drivers10.Fptr.Fptr.loadDriver(String path)
at Atol.Drivers10.Fptr.Fptr..ctor(String libraryPath)
at MySolution.MyClass.MyClass()
...
But when I create a Console Application and put in there exact same code (both versions with path and without), everything works: the device beeps, there are no exceptions.
What could be the reason for that behavior and how to fix this?
The issue may be one of the following
The test application is using 'target platform' different than the console application which works fine. The device driver folders expected for each platform could be different. e.g. Changing the targeted platform from 'any CPU' to 'x64' / 'x86' (depending on the type of OS where you are running it) will help
Try running the test application from admin command prompt. Permissions issue may reflect as 'file not found' (instead of 'file could not be loaded').
Use an assembly binding viewer tool to debug the issue further
Refer to Could not load file or assembly or one of its dependencies for more discussion and inputs on the assembly loading issues.
Thank you samiksc.
The issue was in the test app. The driver and OS that I use are both x64, but the test application is x86. With x86 driver everything works.

MonoDevelop Error: Unknown MSBuild failure. Please try building the project again

Just installed MonoDevelop and I tried to compile a simple "Hello World".
Code I used:
using System;
public class HelloWorld
{
static public void Main ()
{
Console.WriteLine ("Hello Mono World");
}
}
The error that I get is :
EmptyCSharpFile.csproj(1,1): Error: Unknown MSBuild failure. Please try building the project again (EmptyCSharpFile)
My system info:
Ubuntu 16.04 LTS , updated
The MonoDevelop and mono are freshly installed.
Mono JIT compiler version 5.0.1.1
MonoDevelop Version 5.10
I can use mcs and run it but can't use monodevelop to run it.
I did read these similar topics but no response yet:
MonoDevelop Failure "Unknown MSBuild Failure" on Linux ;
C# compile Unknown MSBuild error MonoDevelop Linux ; https://askubuntu.com/questions/73630/could-not-obtain-c-compiler-error-when-using-monodevelop
I too had the same problem when I first installed it , I assume you went straight to file -> new-> file-> general -> emptyc# file.
Instead of that, go to file->new-> solution from there choose Console C# project or simply press ctrl + shift + N choose next ,and give project name, by default the new project has a simple hello world program run it, this time it will work.(it worked for me)

MonoDevelop Failure "Unknown MSBuild Failure" on Linux

I just installed MonoDevelop on my Ubuntu machine, and attemtepted to run a hello world, just to test it out:
using System;
public class HelloWorld
{
static public void Main ()
{
Console.WriteLine ("Hello Mono World");
}
}
...but when building it gives an "Unknown MSBuild failure" error
I have tested Mono before using MonoDevelop and verified that it works.
This is most likely due to an error or errors during compilation, which oddly enough, MonoDevelop is not telling you.
Assuming you have mono itself installed, open a terminal and compile from there(source):
mcs HelloWorld.cs
I have had similar issue with a project that used to work - it seems error is in the package NUnit, however I have not yet found a solution for it - have a look at your packages, perhaps you find your issue there

Python.NET - Name must not be empty

I'm embedding some Python code in C#/.NET and it breaks on this line in Python.NET during import:
public ModuleObject(string name): base() {
if (name==String.Empty)
{
throw new ArgumentException("Name must not be empty!")
}
...
How is it possible that some imported Python module is empty string???
So far this is not reproducible on other machine.
I'm using this branch (locally compiled):
https://github.com/renshawbay/pythonnet
This issue may be caused by relative imports. It appears that this has been patched. For me a fix involved upgrading from pythonnet-2.1.0 to pythonnet-2.2.2
pip install pythonnet --upgrade
References
https://github.com/matthid/pythonnet/commit/28239416e4d30edc0ddd24e77e4181b1c4646c3b
https://github.com/pythonnet/pythonnet/pull/182#issuecomment-222008972
Seaborn 0.5.1 is not correctly installed/uninstalled using Anaconda on Windows 7, 64bit, Python 2.7.
'conda remove seaborn' leaves some seaborn files in site-packages.
EDIT
I just realised why this happened - another process blocked complete uninstallation of seaborn package. this is both for conda and pip. so probably has nothing to do with seaborn or conda.

Error when reading audio file?

I have the following function that I am attempting to use to determine the length of an MP3 file:
public static string GetMP3DurationBackup(string Filename)
{
string Duration = null;
WMPLib.WindowsMediaPlayer w = new WMPLib.WindowsMediaPlayer();
WMPLib.IWMPMedia m = w.newMedia(Filename);
if (m != null)
{
Duration = m.durationString;
}
w.close();
return Duration;
}
I have run into an issue where I get the following error:
Retrieving the COM class factory for component with CLSID
{6BF52A52-394A-11D3-B153-00C04F79FAA6} failed due to the following
error: 80040154..
when I call the above function from my web application (call below):
string test = MediaUtil.GetMP3DurationBackup(#"C:\Temp\Audio\bad.mp3");
But when I call it from a console application test harness I created (exact same call as above) it works fine. I have set the project that contains the function to target x86 in the Build properties, but that did not fix the issue.
Does anyone know why this would happen? Suggestions on where to start to debug this?
UPDATED FOR BOUNTY:
Ok, I've tried a number of things but I am still getting this error. Among other things I have tried the steps below which I felt were the most promising, but no dice:
Went into my registry and confirmed that the value at:
HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{6BF52A52-394A-11d3-B153-00C04F79FAA6}\InprocServer32
is refering to C:\WINDOWS\SysWOW64\wmp.dll
Opened command prompt,
navigated to C:\WINDOWS\SysWow64, ran: regsvr32.exe wmp.dll
I have created a console app test harness and I am able to reproduce the error if I run the test project in x64. If I switch it to x86 it works fine.
Does anyone have any idea of why the above would not resolve the issue? Suggestions on where to look next?
You say it doesn't work in x64, but you try to register the 32-bit version of wmp.dll (C:\Windows\SysWow64 contains 32-bit assemblies).
Try to register the x64 version of wmp.dll, which is located in C:\Windows\System32 on a 64-bit platform.
If you don't have this file then there probably is no 64bit Windows Media Player available for your platform. But there is a workaround:
Create a 32-bit console application that takes the mp3 filename as command line argument and outputs the duration to stdout using Console.WriteLine, then in the webapp, you call the console application and capture the output like in this example on MSDN
Give this lib a whirl. Its fast and has no special requirements for software to be installed on the machine.
http://naudio.codeplex.com/

Categories

Resources