No module named xlsxwriter.workbook -Ironpython - c#

I was trying to call a python code using C# ironpython. But encountering this error.
"No module named xlsxwriter.workbook"
Getting this error on import line only.
While the same code is running in that machine with regular python(version 2.7)
Please help.
from xlsxwriter.workbook import Workbook

This is happening probably because the xmlwriter library you are referring to is installed in the Python 2.7 interpreter, but not in the IronPython distribution.
You would have to download the xmlwriter package, extract it to a folder and run:
python setup.py install
using the IronPython executable - as shown in https://ironpython-test.readthedocs.io/en/latest/install/index.html.
Hope this helps!

Related

IronPython.Runtime.Exceptions.ImportException: 'No module named pandas'

I am running C# project which use result of python file. I'm using IronPython to run python file in visual studio. But the error above come up. Please help me. Many thanks!
Sorry you cant access third party functionalities/packages (like Pandas here)
The reason:
Pandas / numpy use great parts of c code, that is a no good for IronPython (.NET).!!!!
If you need to use the power of pandas, i suggest you to create a dialog (send/reveive datas or by file or by calling an external python prog) between your IronPython Project and a program python including pandas.

C#: using Python.Runtime not found

I tried to embedded a python script using Python for .NET. And in the C# program, I tried to import the class
using Python.RunTime;
but it cannot be found. I cannot find out the reason for that. I have already installed pythonnet in my python interpreter.
I just want to test it out by codes like:
using (Py.GIL)
{
dynamic np = Py.Import("numpy");
MessageBox.Show(np.cos(np.pi * 2));
}
* UPDATE *
I have add reference Python.Runtime.dll in the VS project. And when execute, it got System.BadImageFormatException. I have no clue why that happened.
I hope this answer help others who encountered similar problem.
Add the dll as a reference at the VS. It should be located at locations like "C:\Python27\Lib\site-packages\Python.Runtime.dll." if you have done installation of pythonnet via pip.
System.BadImageFormatException is due to the problem that I set the project to ANY platform, but the python interpreter is 64 bit. Need to change the target platform to match the interpreter.

How can I package my python module and its dependencies such as Numpy/Scipy into one file like dll for C# calls?

In my project, some of the algorithms are implemented in python as Modules for calling and libraries such as Numpy/Scipy are used . Now I need to embed these Modules into my UI(implemented by C# and run in Windows 7). There are two reasons I need to package my python modules into a file like DLL(I don't want to package as an .exe because it is not friendly to embed). The first reason is for easily calling, and the second reason is to protect my algorithm source code. Does anyone have any idea?
Finally, I solve my problem by using nuitka. It compile my module into .pyd file, and I can import the module using pythonnet by adding reference Python.Runtime.dll.
My compile command is:nuitka --module --nofreeze-stdlib --recurse-all --recurse-not-to=numpy --recurse-not-to=sklearn --recurse-not-to=pywt --recurse-not-to=matplotlib --recurse-not-to=matplotlib.pyplot --recurse-not-to=scipy.io myfile.py
and my C# code is:
PythonEngine.Initialize();
IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString("import sys\n");
PythonEngine.RunSimpleString("sys.path.append('C:\\path\\to\\pyd')\n");
PyObject pModule = PythonEngine.ImportModule("modulename");
pModule.InvokeMethod("funcitonname", para1, para2);
Try pythonnet to embed CPython in .NET:
python4.net

How to Create a python Dll file that should be used in C#.net?

I have to create a win32 python dll file which can used in C#.Net code.
And access the classes & functions present in the dll file through C#.
Is there any way to create a win32 python dll?
Please help me out....
You can compile your Python sources to a DLL with pyc.py, in the Samples directory of IronPython. However, you can't load this DLL from C# directly - you'll still need to host IronPython, but then you can reference the DLL with the IronPython engine and import from it.
Try do some investigation about Pyrex. I'm not sure if it will solve your issue, but at least I seen that some guys was trying to get it working.
You don't need any DLL, you just need to load your Python sources using IronPython

Compiling an IronPython code to EXE or DLL

Can I compile an IronPython code to EXE or DLL in a .NET runtime?
Simply use clr.CompileModules in an IronPython script to convert it to a dll file.
Or you can use pyc.py (found inside of your IronPythonInstallDirectory\Tools\Scripts) which can also generate an exe for you also.
If I really, really needed that I would create a wrapper DLL/EXE in another language. The Python code can then be stored as a resource in the DLL that is loaded when the assembly is first accessed.
Yes you can! I posted a Python script which can take an IronPython file, figure out its dependencies and compile the lot into a standalone binary at Ironpython 2.6 .py -> .exe. Hope you find it useful.

Categories

Resources