C
C#•2y ago
sectix

DllNotFoundException help with Pythonnet

Hello! I'm using Pythonnet to try to run some python code in C#, however no matter what I do it seems to say it cannot find a python dll. A great example is giving it an exact path and it failing.
Runtime.PythonDLL = @"C:\Users\(me)\anaconda3\python39.dll";
Runtime.PythonDLL = @"C:\Users\(me)\anaconda3\python39.dll";
which works perfectly fine if that were opened using run; however it prints off an error like this: DllNotFoundException: Could not load C:\Users\(me)\anaconda3\python39.dll.";. Any and all help would be appreciated, I just would like to get this going. I have a large project ahead of me I'd like to tackle and my very first required thing is already being a block. I've gone through countless stack overflow pages and tried countless things myself, even rebuilding the entire dll myself (per one pages 'fix'), but between work and chores I don't have much time to figure this out in a reasonable time span, so again, any help would be appreciated.
24 Replies
phaseshift
phaseshift•2y ago
The error could be a dependency of that dll not being found.
sectix
sectixOP•2y ago
Do you have any experience with Pythonnet, and if you do would you happen to know how to reference those too; then? On documentation all it says to do is to set the Runtime.PythonDLL to it's value, and run PythonEngine.Initialize(). Any ideas if not?
phaseshift
phaseshift•2y ago
I'm not familiar with it, sorry
sectix
sectixOP•2y ago
Upon even further inspection, it's actually two errors.
phaseshift
phaseshift•2y ago
Oh that's basically a field or static init blowing up. You might be running incompatible runtime or something Are you using net core/6 etc or net framework? What is the requirement for this pythonnet?
sectix
sectixOP•2y ago
Targeting .NET 4.7.2, and this says it supports Python from 3.7-3.11, I don't see anything related to C# version requirements; however this is the github page if that helps https://github.com/pythonnet/pythonnet (sorry for making you baby me 😓, I appreciate your help)
GitHub
GitHub - pythonnet/pythonnet: Python for .NET is a package that giv...
Python for .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 develo...
Angius
Angius•2y ago
The package is .NET Standard 2.0, so it should support even your outdated version of .NET Framework
sectix
sectixOP•2y ago
If that's not the issue, have any other idea on what it could be? This is all I'm attempting to run, currently.
Angius
Angius•2y ago
sectix
sectixOP•2y ago
Also getting this error, it appears.
Angius
Angius•2y ago
(to be honest, though, I question the very notion of using Python from C#)
sectix
sectixOP•2y ago
yeah I wish I could just do it all in C#, I'm not very familiar with python* but some of the libraries I want to use are python exclusives by the looks of it so I don't have too much of a choice. I've tried many other paths that don't involve python in C#, but none have yielded any results.
Angius
Angius•2y ago
Out of curiosity, what specific libraries do you want to use? Besides NumPy?
sectix
sectixOP•2y ago
Openai, Transformers. Attempting to make a GPT-2 bot, I'm a bit of an AI enthusiast, actually going to get a degree in machine learning.
Angius
Angius•2y ago
And why not just do it all with Python?
sectix
sectixOP•2y ago
I'm most familiar with C#, and I'd like to make it into a discord bot, with a lot of features, such as reading chats and using them as training data on a schedule, so it can train itself based on a channel's chat and speak in said channel with anyone with access. Doing all of these different things in a language I've never touched before seems a little... not just hard, but not sure if it's a smart idea. I know how to do everything excluding the actual GPT-2 stuff in C#, and the GPT-2 stuff is only half the fight. If I can do that half in python, and the other half in C#, then that's absolutely golden. Hence wanting to get this library to work 🤣
Angius
Angius•2y ago
I'd probably write two separate apps tbh One in Python that does the AI thingamajig And a bot in C# Then just System.Diagnostics.Process.Start("ai-stuff.py") or w/e
sectix
sectixOP•2y ago
How would they communicate, is the question though? If I could get them to communicate that'd be great, and the only solution I can think of is local connection through TCP but that seems needlessly complicated compared to getting this to work no?
sectix
sectixOP•2y ago
no way
Angius
Angius•2y ago
Or, to make code from this example better,
public string RunCommand(string cmd, string args)
{
var start = new ProcessStartInfo {
FileName = "PATH_TO_PYTHON_EXE",
Arguments = $"\"{cmd}\" \"{args}\"",
UseShellExecute = false,// Do not use OS shell
CreateNoWindow = true, // We don't need new window
RedirectStandardOutput = true,// Any output, generated by application will be redirected back
RedirectStandardError = true // Any error in standard output will be redirected back (for example exceptions)
};

using var process = Process.Start(start);
using var reader = process.StandardOutput;

var stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
var result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
return result;
}
public string RunCommand(string cmd, string args)
{
var start = new ProcessStartInfo {
FileName = "PATH_TO_PYTHON_EXE",
Arguments = $"\"{cmd}\" \"{args}\"",
UseShellExecute = false,// Do not use OS shell
CreateNoWindow = true, // We don't need new window
RedirectStandardOutput = true,// Any output, generated by application will be redirected back
RedirectStandardError = true // Any error in standard output will be redirected back (for example exceptions)
};

using var process = Process.Start(start);
using var reader = process.StandardOutput;

var stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
var result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
return result;
}
sectix
sectixOP•2y ago
Was busy working with what you sent, and got pretty far. All I've gotta say is that's pretty fucking cool. I appreciate all the help man, got it to log python outputs.
Angius
Angius•2y ago
Nice
sectix
sectixOP•2y ago
😄 Very much appreciated, I'll close the post assuming I can now. Thanks for all the help @phaseshift, and you too.
Want results from more Discord servers?
Add your server