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.1.1.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

dotnetinteropt-0.1.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dotnetinteropt-0.1.1.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for dotnetinteropt-0.1.1.tar.gz
Algorithm Hash digest
SHA256 051a7b05f3f5ed40a9f069d409af7128ea7cf2269fdac2f2342a9f46306a9338
MD5 081d901a7935ab841d06b09994214205
BLAKE2b-256 08549350d8ab07d26565e54caebe508a289966aa11707e3da5cbf647b7dad3fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dotnetinteropt-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6693bfd46300c47dcb8410022153f025d331b7e4865f9552b329e76e0bb7cfdc
MD5 f64a4b290137c8189bd72eeeff87ab64
BLAKE2b-256 156851161b0b97bbaba7f3492c3a113feebd2eb956b0a06660f217871d0f4095

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