Code Monkey home page Code Monkey logo

pythonnet's Introduction

pythonnet - Python.NET

Join the chat at https://gitter.im/pythonnet/pythonnet stackexchange shield

gh shield

license shield

pypi package version conda-forge version python supported shield

nuget preview shield nuget release shield

Python.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.

Note

The master branch of this repository tracks the ongoing development of version 3.0. Backports of patches to 2.5 are tracked in the backports-2.5 branch.

Calling .NET code from Python

Python.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

  • You must set Runtime.PythonDLL property or PYTHONNET_PYDLL environment variable starting with version 3.0, otherwise you will receive BadPythonDllException (internal, derived from MissingMethodException) upon calling Initialize. Typical values are python38.dll (Windows), libpython3.8.dylib (Mac), libpython3.8.so (most other Unix-like operating systems).
  • 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 = (double)(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.]

Resources

Information on installation, FAQ, troubleshooting, debugging, and projects using pythonnet can be found in the Wiki:

https://github.com/pythonnet/pythonnet/wiki

Mailing list
https://mail.python.org/mailman/listinfo/pythondotnet
Chat
https://gitter.im/pythonnet/pythonnet

.NET Foundation

This project is supported by the .NET Foundation.

pythonnet's People

Contributors

amos402 avatar badsingleton avatar brianlloyd avatar cronan avatar danabr avatar davidanthoff avatar den-run-ai avatar dmitriyse avatar fdanny avatar filmor avatar jfrayne avatar jhonabreul avatar jmlidbetter avatar johnburnett avatar koubaa avatar lostmsu avatar lstratman avatar martin-molinero avatar matthid avatar patstew avatar pkese avatar rickardraysearch avatar slide avatar testrunner123 avatar tiran avatar tminka avatar tonyroberts avatar vmuriart avatar williamsardar avatar yagweb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pythonnet's Issues

Failing to initialize Path confusion and

Environment

  • Pythonnet version: 2.0.4
  • Python version: 3.6.8
  • Operating System: Windows 10
  • .NET Runtime: net5

Details

  • I was trying to run unit tests, but the python initialization kept failing. I set the environment variable and restarted visual studio, but still had the same problem. I found on the Pythonnet documentation that if you set the PythonHome property in the engine it can resolve any path problems that might be occuring. I have multiple versions of python on my system.

    TODO

  • What commands did you run to trigger this issue? If you can provide a
    Minimal, Complete, and Verifiable example
    this will help us understand the issue.

You might be able to reproduce the issue by installing another version of python and have it be in the path. I'm not really sure though. For me I had python 3.9 as my main instance, but was trying to use 3.6.8 for pythonnet.

Required explicit path (HintPath) setting for added package

Always required specify the path explicitly to avoid the error:
The type 'PyObject' is required here and is unavailable. You must add a reference to assembly 'Python.Runtime, Version=1.0.5.30, Culture=neutral, PublicKeyToken=null'.

<HintPath>$(NUGET_PACKAGES)\quantconnect.pythonnet\1.0.5.30\lib\win\Python.Runtime.dll</HintPath>

fsproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net48</TargetFramework>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Main.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="QuantConnect.Lean" Version="2.4.8585" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="QuantConnect.pythonnet" Version="1.0.5.30" />
    <Reference Include="Python.Runtime, Version=1.0.5.30, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>$(NUGET_PACKAGES)\quantconnect.pythonnet\1.0.5.30\lib\win\Python.Runtime.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

Main.fs

namespace Test

open QuantConnect.Data.Market
open QuantConnect.Algorithm

type BasicTemplateAlgorithm() =
    inherit QCAlgorithm()
        override this.Initialize() = ()
        member this.OnData(bar:TradeBars) = ()

Increase initialization timeout

We've starting seeing this exception error happen on and off, let's increase the time out ๐Ÿ‘

2021-05-24T22:48:48.7695468Z ERROR:: WorkerThread.<.ctor>b__7_0(): WorkerThread(): exception thrown when running task System.TimeoutException: Timeout while waiting for assemblies to load
   at Python.Runtime.AssemblyManager.Initialize()
   at Python.Runtime.Runtime.Initialize(Boolean initSigs, ShutdownMode mode)
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv, Boolean initSigs, ShutdownMode mode)
   at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv, Boolean initSigs, ShutdownMode mode)
   at Python.Runtime.PythonEngine.Initialize()
   at QuantConnect.Python.PythonInitializer.Initialize() in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Common/Python/PythonInitializer.cs:line 42
   at QuantConnect.Util.WorkerThread.<.ctor>b__7_0() in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Common/Util/WorkerThread.cs:line 62

Display String Representation in C# Objects Nested in List

Details

The print method in Jupyter doesn't display string from __repr__/__str__ when they are nested in a list:

qb = QuantBook()
tickers = ["ITA", "PPA", "XAR", "DFEN", "SHLD"]
symbols = []

for ticker in tickers:
    symbols.append(qb.AddEquity(ticker, Resolution.Daily).Symbol)

print(symbols)

Displays:

[<QuantConnect.Symbol object at 0x7f9b74b40608>,
 <QuantConnect.Symbol object at 0x7f9b7638ba48>,
 <QuantConnect.Symbol object at 0x7f9b746d7c08>,
 <QuantConnect.Symbol object at 0x7f9b746d71c8>,
 <QuantConnect.Symbol object at 0x7f9b746d7108>]

when it should:

['ITA', 'PPA', 'XAR', 'DFEN', 'SHLD']

How to install QuantConnect's version of pythonnet?

Environment

  • Pythonnet version: Not sure
  • Python version: Python 3.6.8 :: Anaconda, Inc.
  • Operating System: Linux 4814784edf7a 4.19.84-microsoft-standard #1 SMP Wed Nov 13 11:44:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Details

  • Describe what you were trying to get done.

    I am trying to install pythonnet inside the lean docker container.

  • What commands did you run to trigger this issue? If you can provide a
    Minimal, Complete, and Verifiable example
    this will help us understand the issue.

docker run --rm \
    --entrypoint /bin/bash \
    quantconnect/lean:latest -c "pip install git+git://github.com/QuantConnect/pythonnet.git"
  • If there was a crash, please include the traceback here.
  Collecting git+git://github.com/QuantConnect/pythonnet.git
  Cloning git://github.com/QuantConnect/pythonnet.git to /tmp/pip-req-build-pr2g_nr8
  Running command git clone -q git://github.com/QuantConnect/pythonnet.git /tmp/pip-req-build-pr2g_nr8
Building wheels for collected packages: pythonnet
  Building wheel for pythonnet (setup.py): started
  Building wheel for pythonnet (setup.py): finished with status 'error'
  ERROR: Command errored out with exit status 1:
   command: /opt/miniconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-pr2g_nr8/setup.py'"'"'; __file__='"'"'/tmp/pip-req-build-pr2g_nr8/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-26m73jmp
       cwd: /tmp/pip-req-build-pr2g_nr8/
  Complete output (104 lines):
  running bdist_wheel
  running build
  running build_ext
  Checking for updates from https://www.nuget.org/api/v2/.
  Currently running NuGet.exe 4.1.0.
  Updating NuGet.exe to 5.5.1.
  Update successful.
  MSBuild auto-detection: using msbuild version '15.0' from '/usr/lib/mono/msbuild/15.0/bin'.
  Restoring NuGet package UnmanagedExports.1.2.7.
  Restoring NuGet package NUnit.ConsoleRunner.3.7.0.
  Restoring NuGet package NUnit.3.7.1.
    GET https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/unmanagedexports/1.2.7/unmanagedexports.1.2.7.nupkg
    GET https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/nunit/3.7.1/nunit.3.7.1.nupkg
    GET https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/nunit.consolerunner/3.7.0/nunit.consolerunner.3.7.0.nupkg
    GET https://api.nuget.org/v3-flatcontainer/unmanagedexports/1.2.7/unmanagedexports.1.2.7.nupkg
    GET https://api.nuget.org/v3-flatcontainer/nunit/3.7.1/nunit.3.7.1.nupkg
    GET https://api.nuget.org/v3-flatcontainer/nunit.consolerunner/3.7.0/nunit.consolerunner.3.7.0.nupkg
    GET https://api.nuget.org/v3-flatcontainer/nunit.consolerunner/3.7.0/nunit.consolerunner.3.7.0.nupkg
    GET https://api.nuget.org/v3-flatcontainer/unmanagedexports/1.2.7/unmanagedexports.1.2.7.nupkg
    GET https://api.nuget.org/v3-flatcontainer/nunit/3.7.1/nunit.3.7.1.nupkg
    OK https://api.nuget.org/v3-flatcontainer/unmanagedexports/1.2.7/unmanagedexports.1.2.7.nupkg 26ms
  Installing UnmanagedExports 1.2.7.
    OK https://api.nuget.org/v3-flatcontainer/nunit/3.7.1/nunit.3.7.1.nupkg 157ms
  Installing NUnit 3.7.1.
    OK https://api.nuget.org/v3-flatcontainer/nunit.consolerunner/3.7.0/nunit.consolerunner.3.7.0.nupkg 193ms
  Installing NUnit.ConsoleRunner 3.7.0.
    OK https://api.nuget.org/v3-flatcontainer/nunit/3.7.1/nunit.3.7.1.nupkg 512ms
    NotFound https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/unmanagedexports/1.2.7/unmanagedexports.1.2.7.nupkg 1436ms
    OK https://api.nuget.org/v3-flatcontainer/nunit.consolerunner/3.7.0/nunit.consolerunner.3.7.0.nupkg 1463ms
  Adding package 'UnmanagedExports.1.2.7' to folder '/tmp/pip-req-build-pr2g_nr8/packages'
  Adding package 'NUnit.ConsoleRunner.3.7.0' to folder '/tmp/pip-req-build-pr2g_nr8/packages'
    NotFound https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/nunit/3.7.1/nunit.3.7.1.nupkg 1707ms
  Added package 'UnmanagedExports.1.2.7' to folder '/tmp/pip-req-build-pr2g_nr8/packages'
  Added package 'NUnit.ConsoleRunner.3.7.0' to folder '/tmp/pip-req-build-pr2g_nr8/packages'
  Adding package 'NUnit.3.7.1' to folder '/tmp/pip-req-build-pr2g_nr8/packages'
  Added package 'NUnit.3.7.1' to folder '/tmp/pip-req-build-pr2g_nr8/packages'

  NuGet Config files used:
      /tmp/pip-req-build-pr2g_nr8/NuGet.config
      /root/.config/NuGet/NuGet.Config

  Feeds used:
      https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
      https://api.nuget.org/v3/index.json

  Installed:
      3 package(s) to packages.config projects
  sh: umask: I/O error
  clang: warning: /opt/miniconda3/include/python3.6m: 'linker' input unused
  Traceback (most recent call last):
    File "tools/geninterop/geninterop.py", line 292, in <module>
      sys.exit(main())
    File "tools/geninterop/geninterop.py", line 274, in main
      ast = parser.parse(python_h)
    File "/opt/miniconda3/lib/python3.6/site-packages/pycparser/c_parser.py", line 152, in parse
      debug=debuglevel)
    File "/opt/miniconda3/lib/python3.6/site-packages/pycparser/ply/yacc.py", line 331, in parse
      return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
    File "/opt/miniconda3/lib/python3.6/site-packages/pycparser/ply/yacc.py", line 1199, in parseopt_notrack
      tok = call_errorfunc(self.errorfunc, errtoken, self)
    File "/opt/miniconda3/lib/python3.6/site-packages/pycparser/ply/yacc.py", line 193, in call_errorfunc
      r = errorfunc(token)
    File "/opt/miniconda3/lib/python3.6/site-packages/pycparser/c_parser.py", line 1861, in p_error
      column=self.clex.find_tok_column(p)))
    File "/opt/miniconda3/lib/python3.6/site-packages/pycparser/plyparser.py", line 67, in _parse_error
      raise ParseError("%s: %s" % (coord, msg))
  pycparser.plyparser.ParseError: /usr/include/crypt.h:33:6: before: __THROW
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-req-build-pr2g_nr8/setup.py", line 527, in <module>
      zip_safe=False,
    File "/opt/miniconda3/lib/python3.6/site-packages/setuptools/__init__.py", line 144, in setup
      return distutils.core.setup(**attrs)
    File "/opt/miniconda3/lib/python3.6/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/opt/miniconda3/lib/python3.6/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/opt/miniconda3/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/tmp/pip-req-build-pr2g_nr8/setup.py", line 475, in run
      return bdist_wheel.bdist_wheel.run(self)
    File "/opt/miniconda3/lib/python3.6/site-packages/wheel/bdist_wheel.py", line 223, in run
      self.run_command('build')
    File "/opt/miniconda3/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/opt/miniconda3/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/opt/miniconda3/lib/python3.6/distutils/command/build.py", line 135, in run
      self.run_command(cmd_name)
    File "/opt/miniconda3/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/opt/miniconda3/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/opt/miniconda3/lib/python3.6/distutils/command/build_ext.py", line 339, in run
      self.build_extensions()
    File "/opt/miniconda3/lib/python3.6/distutils/command/build_ext.py", line 448, in build_extensions
      self._build_extensions_serial()
    File "/opt/miniconda3/lib/python3.6/distutils/command/build_ext.py", line 473, in _build_extensions_serial
      self.build_extension(ext)
    File "/tmp/pip-req-build-pr2g_nr8/setup.py", line 220, in build_extension
      subprocess.check_call([sys.executable, geninterop, interop_file])
    File "/opt/miniconda3/lib/python3.6/subprocess.py", line 311, in check_call
      raise CalledProcessError(retcode, cmd)
  subprocess.CalledProcessError: Command '['/opt/miniconda3/bin/python', 'tools/geninterop/geninterop.py', 'src/runtime/interop36m.cs']' returned non-zero exit status 1.
  ----------------------------------------
  ERROR: Failed building wheel for pythonnet
  Running setup.py clean for pythonnet
Failed to build pythonnet
Installing collected packages: pythonnet
    Running setup.py install for pythonnet: started
    Running setup.py install for pythonnet: finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /opt/miniconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-pr2g_nr8/setup.py'"'"'; __file__='"'"'/tmp/pip-req-build-pr2g_nr8/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-y0rg3pnr/install-record.txt --single-version-externally-managed --compile --install-headers /opt/miniconda3/include/python3.6m/pythonnet
         cwd: /tmp/pip-req-build-pr2g_nr8/
    Complete output (6 lines):
    usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
       or: setup.py --help [cmd1 cmd2 ...]
       or: setup.py --help-commands
       or: setup.py cmd --help

    error: option --single-version-externally-managed not recognized
    ----------------------------------------
ERROR: Command errored out with exit status 1: /opt/miniconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-pr2g_nr8/setup.py'"'"'; __file__='"'"'/tmp/pip-req-build-pr2g_nr8/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-y0rg3pnr/install-record.txt --single-version-externally-managed --compile --install-headers /opt/miniconda3/include/python3.6m/pythonnet Check the logs for full command output.

Installing from the original repo works. This command:

docker run --rm \
    --entrypoint /bin/bash \
    quantconnect/lean:latest -c "pip install git+git://github.com/pythonnet/[email protected]"

returns:

Collecting git+git://github.com/pythonnet/[email protected]
  Cloning git://github.com/pythonnet/pythonnet.git (to revision v2.4.0) to /tmp/pip-req-build-y7hjxlom
  Running command git clone -q git://github.com/pythonnet/pythonnet.git /tmp/pip-req-build-y7hjxlom
  Running command git checkout -q cc538f6a5deedc7af9ec6ab9d3ea0d22a460b54a
Building wheels for collected packages: pythonnet
  Building wheel for pythonnet (setup.py): started
  Building wheel for pythonnet (setup.py): finished with status 'done'
  Created wheel for pythonnet: filename=pythonnet-2.4.0-cp36-cp36m-linux_x86_64.whl size=85671 sha256=a36565ffde5514e69933089564d0c593303f1358c7a506c2a3a1c78203fa6068
  Stored in directory: /tmp/pip-ephem-wheel-cache-2gtul90l/wheels/14/b9/ea/3547a0e9a89fef0a8c5fddb2519d98a89eae9b2b1d00829498
Successfully built pythonnet
Installing collected packages: pythonnet
Successfully installed pythonnet-2.4.0

MethodBinder PyObject + CLRObject resolution

Ideally pythonNet method binder could detect if a PyObject is actually just a CLRObject (wrapped C# instance), get it's type and try to find the best method match for it, if none, fall back to any method taking a PyObject instead. Similar to the implicit conversion matching system. Else it requires C# side to determine the PyObject type instead to handle it correctly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.