Skip to main content

A high-level .NET interop library for Python

Project description

DotnetInteropt

build status build status build status

This package is a tool to easily install NuGet packages and load the compiled .NET DLL's into python. It uses Pythonnet in the background to load and run the DLL's.

In order to use this package you need to have .NET installed and added to your PATH, verify with:

dotnet --version

Usage

First, install the package:

pip install dotnetinteropt

Add it to the build backend dependencies:

[build-system]
requires = [..., "dotnetinteropt"]

And append the following to your pyproject.toml file:

[tool.dotnetinterop]
# OPTIONAL:

# NuGet dependencies used in the project. Can be omitted when
# no dependencies are needed:
dependencies = [
  [
    "Newtonsoft.Json",  # Replace with the actual NuGet package name
    "13.0.3", # Replace with the actual NuGet package version
  ],
  # Add more dependencies here
]
# name of the package to install it in.
# Replace with the actual package name (needed for multi-package projects)
# Otherwise the package name is inferred from the package name in the pyproject.toml:
package = "dotnetinteropt"
# path to where the DLL files should be copied to (relative to package):
path = "_dotnetinteropt" # Default
# Compiled in release or debug mode:
release = true # Default


[tool.setuptools.package-data]
"*" = ["*.dll"] # include all DLL files from all packages

Create a setup.py file in the root of your project:

from setuptools import setup
from dotnetinteropt.backend import install_nugets

if __name__ == "__main__":
    install_nugets() # Has to be before setup such that it then can include the DLL's
    setup()

In your python code you can now load the DLL's:

from dotnetinteropt import load

# Load the DLL's
load("dotnetinteropt_examples")  # Change to the package where to find the DLL's

# Now you can import the symbols from the DLL's
from Newtonsoft.Json import Formatting
from Newtonsoft.Json import JsonConvert
from System import *
from System.Collections.Generic import Dictionary

To generate the examples run:

make examples

Then run the examples with:

python3 src/dotnetinteropt_examples/hello_dotnet.py
python3 src/dotnetinteropt_examples/json_dotnet.py

Also, check the pyproject.toml file for the example configuration.

Development

Check out the DEVELOPMENT file for more information on how to develop this package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dotnetinteropt-0.2.0.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

dotnetinteropt-0.2.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file dotnetinteropt-0.2.0.tar.gz.

File metadata

  • Download URL: dotnetinteropt-0.2.0.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for dotnetinteropt-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bd7ad082ac318d01511a2ddd01b94a16d0a13ee81f63daabdeb9844d7f5d7bf2
MD5 78e4abf618fb599c222d5ed78b917ca7
BLAKE2b-256 c781093d36929afddfd5076f9f48d2dcf8b16ce130acffe0771232126de2ebd0

See more details on using hashes here.

File details

Details for the file dotnetinteropt-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dotnetinteropt-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3d726faf46a27272d6d418e31cf3039a993230920484962f58b463fb67919fb
MD5 cab774a36750813caa8c098e71dccc90
BLAKE2b-256 127e8e64b68eb71fd3fe0ddfdcdcd9745995e3ad753f1d3a179b856c4ee1d5e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page