Importing a custom dll built with C# into a python project and access the functions inside the dll with the help of pythonnet or open command prompt and type for installation
# install pythonnet
pip install pythonnet- DLL file contains the C# solution
- The python solution using C# dll file
namespace CalcProject
{
public class calculate
{
/// accept two integer parameters a and b
/// return sum of two numbers
/// Add(3, 2) and return 5
public int Add(int a, int b)
{
return a + b;
}
}
}First download, install and configure python. Then use the package manager pip to install on.
- For create dll file of C#, first write C# code then run, after successfully run goto build folder of project file for collect dll file.
this project is developed using all new os, software and tools.
- Operating System : Windows10
- Software : Python3.11 and Visual Studio Code
import clr
# add reference to the library/dll
clr.AddReference("dlls/CalcProject")
# from namespace name import class name
from CalcProject import calculate
# create object of the class
cls_obj = calculate()
# call the function with class object
# the function from dll that you want to access
result = cls_obj.Add(3, 4)
# output from dll file
print('Output from dll file:', result) # 7
# normal python output
print(f'Output from python: {3 + 4 = }') # 3 + 4 = 7The requirements.txt file, lists of all the python libraries that my "Import DLL File Into Python" depends on and installs those packages from the file:
pip install -r requirements.txt
# or
sudo pip install -r requirements.txtPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
The MIT License (MIT)
“Good code is about making things powerful yet readable”
— Md. Ariful Islam