Compiling an IronPython code to EXE or DLL - c#

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.

Related

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

Importing a dll file written in C or C#

I am trying to import a .dll file which is written in C to Microsoft Visual C# Studio 2010?
Any ideas why I keep on getting this error?
Please make sure that the file is accessible and that it is a valid assembly prompt.
You cannot import a reference to a native DLL. Instead you need to use p/invoke to import each function one by one. This can be a rather laborious process if you have a lot of functions so sometimes a C++/CLI wrapper is more convenient.
I suspect you're not using the DllImportAttribute correctly (or at all). See here:
http://msdn.microsoft.com/en-us/library/aa984739(v=vs.71).aspx

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

Run C# .exe with crontab or daemon?

I was wondering if i can run a dll (c#) with crontab ? The dll is compile with mono.
thx :)
-- EDIT --
Well it can be a .exe. I was looking at daemons on mac and linux, do you think I can run .exe as a daemon.
Why not write a mono-based exe that takes the DLL path and entry point method as parameters? The exe would then use reflection to load the DLL and execute the specified method. (You could opt for convention-over-configuration by specifying something like a DllMain method in your DLL which the exe would know to call automatically. Then just one parameter would be required and the intent of your code more obvious.)
Implementing such an applet would give you a utility similar to RunDll in Windows and allow you to run mono DLLs from cron.
You may want to check out the latest mono release and C# Shell (although I would personally make an exe that called the functions you want from the DLL).
http://www.mono-project.com/CsharpRepl
"On startup the csharp shell will load any C# script files and pre-compiled libraries (ending with .dll) that are located in the ~/.config/csharp directory (on Windows this is the value of Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData)).
The assemblies are loaded first, and then the scripts are executed. This allows your scripts to depend on the code defined in the assemblies.
C# script files are merely files that contains statements and expressions, they can not contain full class definitions, those should be stored and precompiled in DLL files. "
Then you can do things like:
csharp> using System;
csharp> Console.WriteLine ("hello");
hello
csharp>

C#'s equivalent of jar files?

Java provides the jar file so that all the class files and jar files are merged into one file.
Does C# provide equivalent/similar functionality?
.NET compiles into dll or exe. You can use ILMerge to merge several dlls/exes into one.
Aren't .NET assemblies just for this?
Remember, you can include resources, etc in it.
Also, assemblies could be combined using ILMerge, and for more complex scenarios you probably should better use ClickOnce or MSI deployment.
For silverlight, there's XAP packages, but I assume you're talking about desktop .NET.
It's called an "assembly".
http://en.wikipedia.org/wiki/.NET_assembly
The jar equivalent in C# (basically in any .Net language) is dll (for class library) and exe (for executable one) or collectively assembly. But one assembly can not include another assembly in the form of dll or exe. But ILMerge do merges two assemblies but not include one in another like jar file.
But there is project published in codeproject (http://www.codeproject.com/KB/install/NARLoader.aspx) you might get interested in. It do stuff like jar files with the .net assemblies.
Not really, C# works with .dll and .lib. If you don't put everything in the same project (all source code), you won't be able to achieve what you probably want to do.
But with ILMerge, you can combine everything into 1 executable for easier distribution if you don't want to have a setup or a compressed file containing all the files needed..
yes C# provides dll
Dynamic-Link Libraries
No, an assembly AFAIK can not include referenced assemblies.

Categories

Resources