Issue with setting up pythonnet in kaggle #2561
-
I am using a kaggle notebook and am trying to use pythonnet there. I try following the steps of the tutorial for dynamic library. To me it looks like I did everything just like in the tutorial, but somehow the module can not be loaded !apt-get update && apt-get install -y mono-complete && apt-get install -y dotnet-sdk-8.0
!pip install pythonnet
import subprocess
import shutil
import tempfile
import urllib.request
import zipfile
import clr
import sys
from pythonnet import load
import System
import os
load("coreclr")
source_code = """
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalcTestNS
{
public class Calculate
{
public int Add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
}
}
"""
project_file_content = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
"""
def get_loaded_assemblies():
"""Retrieves and prints the names of loaded assemblies."""
for assembly in System.AppDomain.CurrentDomain.GetAssemblies():
print(assembly.GetName().Name)
with tempfile.TemporaryDirectory() as temp_dir:
cs_file = os.path.join(temp_dir, "CalcTestNS.cs")
csproj_file = os.path.join(temp_dir, "CalcTestNS2.csproj")
with open(cs_file, "w") as f:
f.write(source_code)
with open(csproj_file, "w") as f:
f.write(project_file_content)
os.system(f'dotnet build "{csproj_file}" -o "{temp_dir}"')
sys.path.append(temp_dir)
list_files_with_absolute_paths(temp_dir)
print("BEFORE............")
get_loaded_assemblies()
clr.AddReference("CalcTestNS2")
print("\n\nAFTER............")
get_loaded_assemblies() When executing my code. The output is something like this
And when I run the following I get from CalcTestNS import calculate
ct = calculate()
print(ct.Add(1,1))
params = [1,2]
print(ct.Sub(*params)) What I have already tried is to remove the namespace or change the name of the namespace or the project / source files. I also tried using google colab instead of Kaggle and I also tried building the dll file on my own PC and uploading it. Where did I make a mistake? Edit: I simplified my code a bit and removed the temporary directory which could have been an error source and ran the code on a local windows machine. Additionally I tried both net6.0 as well as net 8.0. I still get the same error:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
It looks like you are calling the temp project |
Beta Was this translation helpful? Give feedback.
-
I managed to solve the issue! First: I used Second and more importantly: I added |
Beta Was this translation helpful? Give feedback.
I managed to solve the issue!
First: I used
dotnet publish
instead ofdotnet build
since that is the recommended way of creating binary files to be used in other programs.Second and more importantly: I added
<Platforms>x64</Platforms>
to my csproj file. AnyCpu does not work well with PythonNet!