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?
Related
I have Visual Studio 2013 with my main form written in C#.
And I have a python(3.7) script with many functions (using libraries like Pandas, Numpy etc.)
How do I call the Python functions from my C#.net code using iron python?
i.e. on a particular event from .net UI, the python script should be called and return the value as a parameter from python to .net and vice versa
Tried Iron python
Powershell to call the python script from .net.
.Net Code:
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using IronPython.Runtime.Types;
using IronPython.Modules;
ScriptEngine py = IronPython.Hosting.Python.CreateEngine();
ScriptScope scope = py.ExecuteFile("Square.py");
dynamic Excel1 = scope.GetVariable("Excel")();
Excel1.ReadExcel("C:\\filePath\\abc.xlsx");
Python Script:
class Excel:
def ReadExcel(self, Path):
import pandas as pd
df = pd.read_excel (Path)
print (df)
return df
An unhandled exception of type 'IronPython.Runtime.Exceptions.ImportException' occurs in Microsoft.Dynamic.dll
Additional information: No module named pandas
I am attempting to run a simple python script within my .net application using IronPython. I have installed the nuget package into the project (I have NOT installed the ironpython cli on my machine) and have authored this code to handle setting paths, reading output, and setting input.
this.engine = Python.CreateEngine();
this.scope = this.engine.CreateScope();
this.streamOut = new MemoryStream();
this.streamErr = new MemoryStream();
this.engine.Runtime.IO.SetOutput(this.streamOut, Encoding.Default);
this.engine.Runtime.IO.SetErrorOutput(this.streamErr, Encoding.Default);
// this is a locally set environment variable
string pythonRootPath = Environment.GetEnvironmentVariable("PythonPath");
ICollection<string> searchPaths = engine.GetSearchPaths();
searchPaths.Add($#"{pythonRootPath}\Lib\site-packages\");
searchPaths.Add($#"{pythonRootPath}\Lib");
engine.SetSearchPaths(searchPaths);
this.engine.Execute(#"import jinja2");
Executing the script results in the following exception
IronPython.Runtime.Exceptions.ImportException: 'No module named errno'
My C# project has references to the following relevant (to IronPython) assemblies:
IronPython (v2.7.9)
IronPython.Modules
IronPython.SQLite
IronPython.Wpf
Microsoft.Dynamic
Microsoft.Scripting
Microsoft.Scripting.Metadata
My paths appear to be well enough set for my purposes as it's not the import of jinja2 itself that causes the error and I am able to import some standard library modules. However, I've seen that the os package fails to import for the same reason as jinja2. I also cannot import errno directly as expected, but I can import it directly if I run it via the python 2.7 cli so the package is installed.
I'm very much out of ideas here if anyone has any suggestions.
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?
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
I have written the following code and I am getting no module named fcntl when i am running the python script using C#
print "hi how are you"
import nltk
a="hi how are you"
tokens=nltk.word_tokenize(a)
print tokens
Possibly NLTK depends on subprocess module, which is not supported in embedded IronPython. There're already some answers on the subject:
"No module named fcntl" when py script run from c# but works from windows command line
No module named fcntl
sys.path
Also check your sys.path. Probably it is not set automatically for embedded engine. You can set library paths like this:
engine.SetSearchPaths(
new string[]
{
"C:\\Program Files\\IronPython 2.7",
"C:\\Program Files\\IronPython 2.7\\Lib",
"C:\\Program Files\\IronPython 2.7\\Lib\\site-packages",
}
);
sys.platform
Any module, dependent on fcntl, decides to import it only if sys.platform == 'posix'. Debug it's actual value inside your python code and try to force it to "win32" (before any other imports)
import sys
sys.platform = "win32"