Import scikit in C# application - c#

I am trying to import scikit-learn in a C# (console) application. I am using Python Tools for Visual Studio and IronPython 2.7.3.
I managed to run an external python script and I also managed to import numpy by declaring the python path: "C:\Python27\Lib\site-packages\"
However, when it comes to scikit-learn I get an error message:
Oops! We couldn't execute the script because of an exception: No module named _c
heck_build
___________________________________________________________________________
Contents of C:\Python27\Lib\site-packages\sklearn\__check_build:
setup.py setup.pyc setup.pyo
_check_build.pyd __init__.py __init__.pyc
__init__.pyo
___________________________________________________________________________
It seems that scikit-learn has not been built correctly.
If you have installed scikit-learn from source, please do not forget
to build the package before using it: run `python setup.py install` or
`make` in the source directory.
If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.
The file "_check_build.pyd" exists in "C:\Python27\Lib\site-packages\sklearn__check_build\".
My code is based on this article: http://devleader.ca/2013/09/23/visual-studio-c-python-sweet/
The file I am using has only the following code:
from sklearn.svm import SVC
print('Hello Python in C#')
Is it possible to add and use scikit in C#? If yes, could you please provide a workaround?

Looks like scikit-learn requires a C extension, which means it won't run under IronPython.

Related

Importing numpy using IronPython in C#

I am trying to use numpy module functions in my Python code that i run inside my c# application in VS13.
I'm doing this to import the module:
ScriptEngine pyEngine = IronPython.Hosting.Python.CreateEngine();
ScriptScope pyScope = pyEngine.CreateScope();
pyEngine.Execute("import numpy", pyScope);
however it says "No module named numpy".
I was looking for the solution and found this:
How to install numpy and scipy for Ironpython27? Old method doens't work
Using Nilsters answer I managed to install numpy and im able to use it when i run ipy from cmd. However i dont know how to use it in my c# app in VS.
I've been looking through the files and found
\IronPython 2.7\DLLs\NumpyDotNet.dll
and also:
\IronPython 2.7\Lib\site-packages\numpy\
How do i make it possible for my python code to import numpy?

IronPython cannot import module cv2 (OpenCV)

First of all, the python code is perfectly working in PyCharm and in command prompt. So, Cv2 module is installed well on my Windows machine.
But when I run by IronPython Script engine, it failed as below.
IronPython.Runtime.Exceptions.ImportException: No module named cv2
I setup IronPython engine as below. Note that site-packages has cv2.pyd file.
var engine = Python.CreateEngine();
List<string> pathes = engine.GetSearchPaths().ToList();
pathes.AddRange(new[]
{
#"C:\Python27\Lib\", #"C:\Python27\Lib\site-packages\"
});
engine.SetSearchPaths(pathes);
dynamic py = engine.ExecuteFile("sample.py"); // <- Exception occurred here.
I guess engine.Setup.FileExtensions has only .py file, so that cv2.pyd is not recognized. But, I hardly figure out how to add .pyd to the setup.
Or, is there anything that I missed?
I think you did nothing wrong, but *.pyd files are just not working for IronPython by default. Just checkout IronClad or this so article: https://stackoverflow.com/a/1231131/2630261

Build TagLib# DLL from source and make it COM Visible to PHP

Hello I want to scan audio-video files and store their metadata in a database using php. I found this Command-line wrapper that uses TagLib.dll compiled by banshee developpers to do the job. It's works fine but it limited by the functions implemented. I want to access directly to the dll methods via PHP.
In PHP we have a function (DOTNET) that allows me to instantiate a class from a .Net assembly and call its methods and access its properties like this :
/*$obj = new DOTNET("assembly", "classname");*/
$stack = new DOTNET("mscorlib", "System.Collections.Stack");
$stack->Push(".Net");
$stack->Push("Hello ");
echo $stack->Pop() . $stack->Pop();
//Returns string(10) "Hello .Net";
Here is the Taglib# project sources in github
I saw many questions relatives to PHP-DLL-COM and there is some recommendations :
Make the dll comVisible;
Register the dll with regsvr32;
Use a module definition file similar to
;
;
DESCRIPTION "Simple COM object"
EXPORTS
DllGetClassObject PRIVATE
DllCanUnloadNow PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
My question is : How can I build the DLL and use its method via PHP ?
My config :
OS
Windows Server 2012 R2 Standard Edition i586
Apache :
Apache/2.2.21 (Win32) DAV/2 PHP/5.4.42 mod_ssl/2.2.21 OpenSSL/0.9.8r
PHP
PHP Version : 5.4.42
Arch : x86
Compiler : MSVC9 (Visual C++ 2008)
COM support : enabled
DCOM support : disabled
.Net support enabled
Microsoft Visual Studio 2013
Give the following steps a try:
Download taglib source code from github
Remove the ApplicationIcon tag from the .csproj file and open .sln in Visual Studio
Unload the test project (you don't need to build this)
Right-click on taglib-sharp project --> properties --> build --> enable 'Register for COM interop'. Also clear the conditional compilation symbols textbox so that you don't have to bother with downloading SharpZipLib for now.
Observe #1: in project properties --> Application --> Assembly information --> 'Make assembly COM visible' is checked
Observe #2: in project properties --> Application --> target framework is set to 3.5 (make sure you leave it as that)
Build the project (F6)
Read the contents of the output window to see some warnings
Now that you have src\taglib-sharp.dll you need to register it into the global assembly cache in order for the DOTNET class to find it. See PHP DOTNET hell for details if you are not familiar with this.
If all is good, you can get yourself a copy of SharpZipLib, and reenter the HAVE_SHARPZIPLIB conditional compilation symbol --> rebuild --> redeploy to GAC --> and be a happy man! :)
You may compile you DLL with .NET Framework 3.5 if not, PHP do not be able to load it via DOTNET class.
Download .NET 3.5

Import DLL c++ on VSC# 2010 (AdminPaq SDK )

I'm trying to import a library to my WCF project, the DLL was provided by Adminpaq.
i'm using the next following code:
[DllImport("MGW_SDK.dll", EntryPoint = "fInicializaSDK")]
public static extern int fInicializaSDK();
When i debug the app say the next error : Unable to load DLL The specified module could not be found.
I've ready on web that the error could be by dependece but on the SDK documentation display a list of some files that must be added to the root project i'm currently adding al of them
Some info:
Dll is build on: c++
Proyect type: c# Application service MCF
I'm using: Windows 7 ultimate x64
Any further information required please don't hesitate to ask.
You are right, usually it is related to some other library that your MGW_SDK.dll depends on. Try these:
1) Install the Microsoft Visual C++ redist (2005, 2008 or 2010, depending on which VS was used to build that dll)
2) If that does not help, try using dependency walker tool to see which files are missing (http://www.dependencywalker.com)
I hope that helps.
You must type the full and original path of the DLL, for example: #"C:\Program Files (x86)\Compacw\AdminPAQ\MGW_SDK.dll"
you must install AdminPaq and have a registered license, after that the error will dissapear

C# dll references produce a classnotfound exception

I used IKVM to convert slf4j to dll's for use in a c# program. It's throwing me an exception. (NoClassDefFoundError was unhandled org.slf4j.LoggerFactory)
The code is as follows
acceptor = new SocketAcceptor();
connectionHandler = new ConnectionHandler();
serverlistenerPort = 32156;
SocketAcceptorConfig sac = new SocketAcceptorConfig();
sac.getSessionConfig().setTcpNoDelay(false);
sac.setReuseAddress(true);
sac.setBacklog(100);
//The following line is what seems to hate me.
acceptor.bind(new InetSocketAddress(serverlistenerPort), connectionHandler, sac);
Connection Handler is a class I have that overrides IoHandler(a mina class) I have triple checked that I have all the IKVM dll's referenced, and I've searched every possible combination of terms on Google for a solution, but when all the solutions are for Java and they just need to reference .jar files. I'm certain I have all the .dll's referenced (This works as a java program using jars)
I had the same issue. I used this command to get the dll compiled:
ikvmc -sharedclassloader file1.jar ...... file8.jar
should be OK for what I've read, shouldn't it?
And then used this to get the dll into the global cache:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64> gacutil -i "C:\ikvm-7.1.4532.2\bin\IKVM.OpenJDK.Core.dll"
and same command to put all the partial libraries needed

Categories