2

I'm trying to make some of my Python scripts callable from .NET. I've found out that IronPython is a good tool to accomplish this. So I've tried to use it, but now I have the following questions:

  1. How can I install packages to IronPython? My existing Python code uses them, so I need to get them installed. I've tried to follow this guide to install pip, but got Process is terminated due to StackOverflowException. and haven't found a way to solve the issue.
  2. Assuming I'll be able to install packages, is it still true that packages with C/C++ bindings cannot be used in IronPython? Because on the FAQ page is written that NumPy and SciPy can't be installed, but the page seems to be rather old.
  3. What about accessing Python code from other .NET code? The documentation page says that I have to use the DLR Hosting APIs, because assemblies compiled from IronPython are not CLS-compliant. But I can't get an idea how to make it work with packages.
  4. Maybe there is another way to call Python code from .NET?

Thanks in advance.

3
  • 1
    python4.net Commented Jun 2, 2017 at 11:28
  • @denfromufa Thank you for a possible solution. As I see Python for .NET needs a full CPython installation. Is there a way to "package" (not sure how to call this process right) both the interpreter and installed packages into a "file" and use this "file" in Python for .NET a the interpreter? I hope my idea is understandable. Commented Jun 2, 2017 at 12:17
  • 1
    Yes, you need to point to location of stand-alone CPython interpreter using pythonhome and path env. vars when calling from .NET Commented Jun 2, 2017 at 13:00

1 Answer 1

2

OK, I think I've figured things out.

  1. The problem with pip was due to the fact that I've tried to use ipy64. Using ipy has solved the problem.
  2. Yes, today it is impossible to use packages with C/C++ bindings.
  3. Lets assume that pip and requests are installed using ipy -X:Frames -m ensurepip and ipy -X:Frames -m pip install requests (more info about pip in IronPython can be found here). requests can be used using the DLR Hosting APIs in the following way:

    var engine = IronPython.Hosting.Python.CreateEngine(new Dictionary<string, object> {{"Frames", true}});
    var searchPaths = engine.GetSearchPaths();
    searchPaths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib");
    searchPaths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib\site-packages");
    engine.SetSearchPaths(searchPaths);
    engine.CreateScriptSourceFromString("import requests; r = requests.get('http://www.google.com/'); print r.status_code").Execute();
    

    More info about hosting Python can be found in the IronPython documentation file (default location is C:\Program Files (x86)\IronPython 2.7\Doc\IronPython.chm) under the Hosting IronPython section.

  4. As denfromufa has suggested one can use Python for .NET to call CPython code from .NET.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.