c#


Can you execute Python code in C#? [duplicate]


Is there a way to call Python code, using IronPython I assume, from C#? If so, how?
The process is simple, especially in a C#/.NET 4 application where support for dynamic languages have been improved via usage of the dynamic type. But it all ultimately depends on how you intend to use the (Iron)Python code within your application. You could always run ipy.exe as a separate process and pass your source files in so they may be executed. But you probably wanted to host them in your C# application. That leaves you with many options.
Add a reference to the IronPython.dll and Microsoft.Scripting.dll assemblies. You'll usually find them both in your root IronPython installation directory.
Add using IronPython.Hosting; to the top of your source and create an instance of the IronPython scripting engine using Python.CreateEngine().
You have a couple of options from here but basically you'd create a ScriptScope or ScriptSource and store it as a dynamic variable. This allows you to execute it or manipulate the scopes from C# if you choose to do so.
Option 1:
Using CreateScope() to create an empty ScriptScope to use directly in C# code but usable in Python sources. You can think of these as your global variables within an instance of the interpreter.
dynamic scope = engine.CreateScope();
scope.Add = new Func<int, int, int>((x, y) => x + y);
Console.WriteLine(scope.Add(2, 3)); // prints 5
Option 2:
Using Execute() to execute any IronPython code in a string. You can use the overload where you may pass in a ScriptScope to store or use variables defined in the code.
var theScript = #"def PrintMessage():
print 'This is a message!'
PrintMessage()
";
// execute the script
engine.Execute(theScript);
// execute and store variables in scope
engine.Execute(#"print Add(2, 3)", scope);
// uses the `Add()` function as defined earlier in the scope
Option 3:
Using ExecuteFile() to execute an IronPython source file. You can use the overload where you may pass in a ScriptScope to store or use variables defined in the code.
// execute the script
engine.ExecuteFile(#"C:\path\to\script.py");
// execute and store variables in scope
engine.ExecuteFile(#"C:\path\to\script.py", scope);
// variables and functions defined in the scrip are added to the scope
scope.SomeFunction();
Option 4:
Using GetBuiltinModule() or the ImportModule() extension method to create a scope containing the variables defined in said module. Modules imported this way must be set in the search paths.
dynamic builtin = engine.GetBuiltinModule();
// you can store variables if you want
dynamic list = builtin.list;
dynamic itertools = engine.ImportModule("itertools");
var numbers = new[] { 1, 1, 2, 3, 6, 2, 2 };
Console.WriteLine(builtin.str(list(itertools.chain(numbers, "foobar"))));
// prints `[1, 1, 2, 3, 6, 2, 2, 'f', 'o', 'o', 'b', 'a', 'r']`
// to add to the search paths
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(#"C:\path\to\modules");
engine.SetSearchPaths(searchPaths);
// import the module
dynamic myModule = engine.ImportModule("mymodule");
You can do quite a lot hosting Python code in your .NET projects. C# helps bridging that gap easier to deal with. Combining all the options mentioned here, you can do just about anything you want. There's of course more you can do with the classes found in the IronPython.Hosting namespace, but this should be more than enough to get you started.

Related Links

datetime asp mvc 4 razor
ASP.NET MVC - How to hide or Show a link/button based on logged in User's Role permission?
How to restore System.__ComObject state from client on server-side?
How to bind Div to Property on a model in MVC 3
With Autofac, how do I enable property injection on a single mvc controller after RegisterControllers has been called?
C# Multi-threaded app - structure?
xml xsd validation and malformed xml
C# overriding old objects in a loop [closed]
Extracting a key-value pair from a web service XML response
How to deal with deprecated values in (country-)code lists
How to get the web site assembly from a library
AssertWasCalled() equivalent where method not known at compile-time
How to minimize application to tray by using command line argument
Unable to add dictionary item to listBox
Iterate through xml elements with powershell
Is QueueUserWorkItem with lock correct implementation?

Categories

HOME
oauth-2.0
pact
optimization
compression
pelican
snappy
abc
dronekit
nagios
arcgis
hex
spss
azure-resource-manager
ms-application-insights
kohana
open-search-server
markdown
google-api-oauth
vsixmanifest
phpexcel
mongoimport
google-closure-compiler
template10
audio-streaming
caldav
react-jsx
datasource
restructuredtext
lotusscript
multipart
fastboot
tcpdf
jopendocument
angular2-changedetection
large-files
sqldependency
custom-fields
hierarchy
kundera
login-required
urhosharp
loopback
microstrategy
webspeech-api
perf
armadillo
sknode
autodesk-data-management
grails-2.0
chrome-custom-tabs
dna-sequence
fluent-nhibernate-mapping
playframework-2.3
ajv
grib
nls
virtualenvwrapper
jira-agile
geokit
init
vitamio
avi
audiorecord
shtml
mod
nugetgallery
periodic-processing
methodology
markerspiderfier
webservices-client
esb-toolkit-2.1
radgrid
genero
shunting-yard
fedena
fxmlloader
system.net
rpt
visual-studio-setup-proje
ant-contrib
slimscroll
dayofweek
acrofields
data-layers
mahara
complete
nsnumber
wpa
google-checkout
proj4
spawn
cloudmade
freeimage
netbiscuits
sqlbuddy
cherokee
soapexception
mediastreamsource
system-analysis
newspeak
glassfish-embedded
dojox.gfx
user-preferences
html-help-workshop

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App