Importing numpy using IronPython in C# - 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?

Related

C#, IronPython error: IronPython.Runtime.Exceptions.ImportException - No module named requests

Using VS 2019 I installed the nuget packages IronPython v2.7.11 and DynamicLanguageRuntime v1.3.0 and am trying to run a python script. It ran fine until I added imports to the script:
import requests, time, socket
import random
Here’s the C#:
static void Main(string[] args)
{
DateTime start = DateTime.Now;
ScriptEngine engine = Python.CreateEngine();
var searchPaths = engine.GetSearchPaths();
// Path to where everything is installed for this Python enviroment
searchPaths.Add(#"C:\Users\George\.conda\envs\spenv2");
int url_id = 1701;
string url = "https://www.yellowpages.com/search?search_terms=Health&geo_location_terms=Aliso+Viejo%2C+CA";
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(#"D:\Apps\Data Destinations\Scraping\ScrapeProjects\Job1.v4_For_IronPython_CSharp\ScrapeUrlPages.py", scope);
dynamic testFunction = scope.GetVariable("scrape_all_pages_for_this_search");
dynamic result = testFunction(url_id, url);
Console.WriteLine(result);
}
I found a few posts regarding similar issues and the solution most of them was to add search paths. Which I did:
searchPaths.Add(#"C:\Users\George\.conda\envs\spenv2");
This is where everything is installed for the Python environment, spendv2, is being used.
Any recommendations on what I need to do?
Thank you.
The problem is an incompatibility between IronPython and Python and the more I dug into this the more problems I found.
Not documented anywhere but I found someone from the IronPython crew who pointed out that IronPython 2.7.11 "Must" interact with Python 2.7. Hmmm. 2.7 hit end-of-life a long time ago and you can't even download it from the Python site.
Not documented anywhere that I could find but IronPython 3.4 (just released) "Must" interact with Python 3.4, and yes, 3.4 hit end-of-life a long time ago and you can't even download it from the Python site. I find this odd that a new release of something would be based on something obsolete.
I was able to get Python 2.7.n from nuget and after getting everything setup and configured, and set all the new paths, I was able to get c# to execute a python script via ironpython. The script had the “import requests” and it did run – where as before when attempting to execute the script I would get the error:
IronPython.Runtime.Exceptions.ImportException - No module named requests
The script runs now – yippy - but – the python script gets an exception when calling the requests.get() method. The exception is:
"not expecting type '<type 'bytes'>'" object {string}
Evidently, from my searches, "not expecting type '<type 'bytes'>'" is a result of an incompatibility with the requests module and IronPython. There are about 5 well known python modules that are not compatible with IronPython so when it get called it returns a blob of bytes.
In conclusion, to the best of my knowledge you cannot call python requests from c#
I hope this helps someone as it cost me several long painful days.
What I see is you're using the get for the searchpaths so you load the collection of strings but what is missing is the engine.SetSearchPaths(searchPaths); to get it working.
I know that this is an old post, but I found something that works for me that can help in future questions about this.
I installed IronPython.StdLib from the NuGet Package.
I downloaded the needed library (Tkinter) with my IDE (PyCharm) and then I added the folder to the Lib folder in my C# project.
I added
var libs = new[] {#"{PATH OF YOUR PROJECT}\Lib"};
engine.SetSearchPaths(libs);
And that works for me.

Calling C# code within Python3.6

with absolutely no knowledge of coding in C#, I wish to call a C# function within my python code. I know there's quite a lot of Q&As around the same problem, but for some strange reason, i'm unable to import a simple c# class library from a sample python module.
Here's below as to what i've done -
C# Class Library setup
I'm using the VS 2017 CE.
I create a new project TestClassLibrary under the type of ClassLibrary(.NET Standard)
The classes inside the project are as follows -
MyClass.cs
using System;
namespace TestClassLibrary
{
public class MyClass
{
public string function()
{
return "Hello World!";
}
}
}
This was built successfully, generating the .dll file under the \bin\Debug\netstandard2.0 dir as TestClassLibrary.dll
Now, I switch over to python3.6 (running on a virtualenv, backed with pythonnet 2.3.0)
main.py
import sys
sys.path.append(r"<Ablsloute Path to \bin>\Debug\netstandard2.0")
import clr
clr.AddReference(r"TestClassLibrary")
from TestClassLibrary import MyClass
When i Run python main.py, the code fails with the error -
Traceback (most recent call last):
File "main.py", line 6, in <module>
from TestClassLibrary import MyClass
ModuleNotFoundError: No module named 'TestClassLibrary'
Should the code be -
import sys
sys.path.append(r"C:\Users\DELL\source\repos\TestClassLibrary\TestClassLibrary\bin\Debug\netstandard2.0")
import clr
clr.AddReference("TestClassLibrary.dll")
from TestClassLibrary import MyClass
I get -
clr.AddReference("TestClassLibrary.dll")
System.IO.FileNotFoundException: Unable to find assembly 'TestClassLibrary.dll'.
at Python.Runtime.CLRModule.AddReference(String name)
But when i ran the code below, the code runs as expected -
import clr
clr.AddReference(r"System.Windows.Forms")
from System.Windows.Forms import MessageBox
MessageBox.Show("Hello World!")
I've no idea of what i might be missing :(
This is really janky but how I like to do things that are personal projects.
Python lets you send stuff to the command line really easily. C# can be run from command line. You probably see where I'm going with this.
Try adding C sharp to PATH. import os to python. then use this line of code when you want to run the C# script:
os.system("csc nameofscript.cs")
perhaps I've misunderstood, but this is how I would make it work on my machine

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

Import scikit in C# application

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.

how to reference compiled python code in IronPython?

we need to reference .py code from C#. This was solved using IronPython 2.6
The issue arises from the fact that .py code uses 'import customlib' which is a library compiled into customlib.pyc
IronPython gives error: IronPython.Runtime.Exceptions.ImportException: No module named customlib
attempted solution:
in python code add reference like this:
import sys
sys.path.append('c:\path to customlib')
that seems to work for other .py files but not for .pyc
Questions:
1) how do you reference .pyc in IronPython?
2) If its not possible, what are the alternatives to use .py and .pyc in C#.Net ?
(obviously we don't have .py source code form customlib.pyc)
C# code that works for .py but not if .py imports .pyc:
ScriptEngine engine = Python.CreateEngine();
ScriptScope pyScope = null;
ScriptSource ss = null;
...
pyScope = engine.CreateScope();
ss = engine.CreateScriptSourceFromFile(#"C:\test.py");
ss.Execute(pyScope);
...
int result = (int)engine.Operations.InvokeMember(pyScope, "funcName")
thanks!
*.pyc files are CPython specific. You'll have to decompile them or invoke CPython.
For decompiling try:
http://sourceforge.net/projects/unpyc/
Free Python decompiler that is not an online service?

Categories