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.
Перейти к файлу
yagweb 550a02737f add a scope class to manage the context of interaction with Python an… (#381)
* 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
2017-06-09 08:56:22 -05:00
.github Fix paths in PULL_REQUEST_TEMPLATE.md 2017-01-22 04:08:13 -07:00
ci Build conda recipe on new tags 2017-03-11 08:33:14 -07:00
conda.recipe Msbuild15 patch (#435) 2017-03-22 20:31:26 -07:00
demo WPF DynamicGrid python and XAML layout files (#280) 2017-03-23 10:45:27 -07:00
src add a scope class to manage the context of interaction with Python an… (#381) 2017-06-09 08:56:22 -05:00
tools Fixing Travis CI for mono 5.0 (#471) 2017-05-24 09:10:21 -05:00
.bumpversion.cfg Bump version: 2.3.0.→ 2.4.0.dev0 2017-03-11 08:07:06 -07:00
.editorconfig improve tests.pyproj for intellisense and running tests (#395) 2017-02-24 14:15:40 -07:00
.gitignore Add Coverity badge 2017-02-17 09:01:52 -07:00
.mention-bot Update .mention-bot 2017-03-28 13:11:36 -05:00
.travis.yml Temporary disable Codecov flags 2017-03-01 21:40:22 -07:00
AUTHORS.md Updated CHANGELOG and AUTHORS (#462) 2017-05-11 09:47:47 +02:00
CHANGELOG.md documentation update for keyword arguments (#483) 2017-06-07 14:45:35 -05:00
CONTRIBUTING.md Clean-up README/CONTRIBUTING 2017-01-14 15:44:34 -07:00
LICENSE Update LICENSE year & include in recipe 2017-02-21 20:03:49 -07:00
README.md documentation update for keyword arguments (#483) 2017-06-07 14:45:35 -05:00
appveyor.yml Unset PYTHONHOME in AppVeyor 2017-03-02 18:03:59 -07:00
pythonnet.sln Add PY3 project/sln configs 2017-02-02 11:32:47 -07:00
requirements.txt Use Codecov report flags 2017-02-16 19:32:51 -07:00
setup.cfg Add timing to detect slow tests on pytest 2017-03-04 19:51:18 -07:00
setup.py Fixing Travis CI for mono 5.0 (#471) 2017-05-24 09:10:21 -05:00
tox.ini Remove check-manifest 2017-02-14 04:55:04 -07:00

README.md

pythonnet - Python for .NET

appveyor shield travis shield codecov shield coverity shield

license shield pypi package version python supported shield stackexchange shield slack

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, eg mod.func(args).
  • Use mod.func(args, Py.kw("keywordargname", keywordargvalue)) or mod.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.]