550a02737f
* add a scope class to manage the context of interaction with Python and simplify the variable exchanging * Deprecate public RunString Had to remove defaults to disambiguate call on `internal RunString`. Can re-add after removing `public RunString` Closes #401 * Rename several methods and add three methods Referring to IronPython, change the name of the methods Get, Exists, SetLocal, DelLocal to GetVariable, ContainsVariable, SetVariable, RemoveVariable. Hidden the methods SetGlobalVariable, RemoveGlobalVariable. Add a new method 'Compile' to compile string into ast, the ast can be seen as the ScriptSource of IronPython. Add two new methods 'Execute' and 'Execute<T>' to execute an ast and obtain the result, corresponding to the 'Execute' method of IronPython. * rebased * Rebased update * format cleanup * create unnamed pyscope, make PyScope.GILState save remove method GetInstHandle add function to create unnamed pyscope add a field isDisposed for PyScope.GILState to make it more save * fixup! create unnamed pyscope, make PyScope.GILState save * remove GIL and rebased * Add several methods add ImportScope: a scope can import variable from any scope, equivalent to python 'import * from mod' add dynamic member support add an OnDispose event remove the field ‘globals’ referring to python module put the scope class in a new file Unit test: TestThread uses scope function replacing Exec/Eval to speed up the execution. * add a Variables method * fixup! add a Variables method * remove private method _GetVariable * add unit test for Variables() method * add several methods and rebased Add an optional dict parameter for Eval/Exec/Execute methods Add a new field obj which point to a Python Module (same as pyobject) Add a static method New Rename the old ImportScope method to ImportAllFromScope Add a new ImportScope method and add a unit test for it Rename the CreateScope method to NewScope * add a new class PyScopeManager * cleaned up the Import methods * updated according to filmor's comments * fixup! updated according to filmor's comments * Get/Set Methods renamed |
||
---|---|---|
.github | ||
ci | ||
conda.recipe | ||
demo | ||
src | ||
tools | ||
.bumpversion.cfg | ||
.editorconfig | ||
.gitignore | ||
.mention-bot | ||
.travis.yml | ||
AUTHORS.md | ||
CHANGELOG.md | ||
CONTRIBUTING.md | ||
LICENSE | ||
README.md | ||
appveyor.yml | ||
pythonnet.sln | ||
requirements.txt | ||
setup.cfg | ||
setup.py | ||
tox.ini |
README.md
pythonnet - Python for .NET
Python for .NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful application scripting tool for .NET developers. It allows Python code to interact with the CLR, and may also be used to embed Python into a .NET application.
Calling .NET code from Python
Python for .NET allows CLR namespaces to be treated essentially as Python packages.
import clr
from System import String
from System.Collections import *
To load an assembly, use the AddReference
function in the clr
module:
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form
Embedding Python in .NET
- All calls to python should be inside
a
using (Py.GIL()) {/* Your code here */}
block. - Import python modules using
dynamic mod = Py.Import("mod")
, then you can call functions as normal, egmod.func(args)
. - Use
mod.func(args, Py.kw("keywordargname", keywordargvalue))
ormod.func(args, keywordargname=keywordargvalue)
to apply keyword arguments. - All python objects should be declared as
dynamic
type. - Mathematical operations involving python and literal/managed types must
have the python object first, eg.
np.pi * 2
works,2 * np.pi
doesn't.
Example
static void Main(string[] args)
{
using (Py.GIL())
{
dynamic np = Py.Import("numpy");
Console.WriteLine(np.cos(np.pi * 2));
dynamic sin = np.sin;
Console.WriteLine(sin(5));
double c = np.cos(5) + sin(5);
Console.WriteLine(c);
dynamic a = np.array(new List<float> { 1, 2, 3 });
Console.WriteLine(a.dtype);
dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype=np.int32);
Console.WriteLine(b.dtype);
Console.WriteLine(a * b);
Console.ReadKey();
}
}
Output:
1.0
-0.958924274663
-0.6752620892
float64
int32
[ 6. 10. 12.]