0

I just started accessing py-files from C# via IronPython and for simple files it worked very well. However, accessing py-files that were used in a Visual Studio Python project is not quite as easy, because the pyproj-file configures imports and requirements and I am having difficulties replicating all of that with IronPython.

I basically want to access everything the Python software can do by invoking its modules from C# with IronPython. Does anyone know what the proper approach to do that is?

I tried just adding everything inside of "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Lib" to my search path, including subdirectories. I successfully gathered all the paths and added them to the engine. I tested it by accessing a few MS shared modules and I don't get the "No module named XYZ" error, but mostly a generic "token error" instead, so putting the paths into the engine must've worked at least.

But what else do I have to do to get the software to work? Am I merely missing some further include paths or is there something else I'm missing, too?

edit: I just found a comment in this answer saying that IronPython isn't compatible with Python 3.x yet, and said comment responded to someone complaining about token errors similar to what I've been seeing. So I guess IronPython is out, then? I'm too much of a newbie to know whether for sure, e.g. there might be ways to make it compatible? I don't know.

Alex
  • 1,198
  • 2
  • 12
  • 26
  • We are using [Python.Net](https://github.com/pythonnet/pythonnet) for scripting language in our WPF application – JL0PD Feb 12 '21 at 13:20
  • Thank you, I will try it out. – Alex Feb 12 '21 at 15:12
  • @JL0PD I can't find any code that explains how to import a custom py-file. When I use "PythonEngine.ImportModule" I always get "Python.Runtime.PythonException: 'ModuleNotFoundError", even when using an absolute path like "D:\\path\\to\\test.py". I also tried using "\\\\" instead of "\\", because someone on the internet said so, with the same result. Do you have working sample code, please? – Alex Feb 22 '21 at 09:48
  • I can't share our code, neither produce minimal sample because of complexity. If you want to import module use `PythonEngine.ModuleFromString`. This method will give you `PyObject` that represents this module and you need to save it to the dictionary of modules given by the `Runtime.PyImport_GetModuleDict`. If you want to execute script than use `PythonEngine.Compile` and pass result to `PythonEngine.Execute`. All of this should be done within `Py.GIL()`. – JL0PD Feb 22 '21 at 12:06
  • Okay, thanks for the info. I was kinda hoping for something as simple as the example in [here](https://pypi.org/project/pythonnet/), but loading my own file instead of numpy. `ModuleFromString` can be an option if I load the files into a string via C#, because then the paths will be reliable, but this feels like a work-around and that Python.net really should be able to do something as simple as loading files. – Alex Feb 22 '21 at 12:40
  • maybe worth a try to get `sys` and append `D:/path/to/test/folder` to `sys.path`, e.g. `((dynamic)Py.Import("sys")).path.append(scriptsPath)`. [Docs](https://docs.python.org/3/library/sys.html#sys.path) – JL0PD Feb 22 '21 at 14:28
  • Wow, so it was that easy after all in the end. XD Thanks a lot for the help! – Alex Feb 22 '21 at 14:53

0 Answers0