简体   繁体   中英

How should I set up a Visual Studio solution if I want to make a call from a C# project into Python code, with external libraries?

I'm working on an existing C# solution in Visual Studio 2019 and I want to add some data massaging code in Python, using the external Regex library (which has support for approximate regex capturing via TRE bindings). Additionally, I want to make sure everything I install can be automatically rebuilt from the source repo, like with Nuget packages.

First I tried adding an IronPython standalone project to the solution, and then adding a dependency to it from my main C# project. This gave me the unhelpful error:

A reference to 'ironpython-project' could not be added.

I'm a little disappointed because I thought the point of IronPython was to compile to a .NET assembly that I could just reference from my C# code.

I tried the same with an unmanaged Python project and got the same error.

Next I tried just putting a python script inside the C# project and loading it at runtime with IronPython. This sort of works, and I get debugging, but then I wasn't able to figure out how to add the regex library I wanted. I tried installing IronPython locally and running ipy.exe, but I got this error:

error: Python was built with Visual Studio version 6, and extensions need to be built with the same version of the compiler, but it isn't installed.

So then I tried writing the Python script as a separate project again, and just calling into it from the main code via the command line. However, it seems like each project gets "built" into an entirely separate folder, so I can't just call "sideways" into the project next door, I have to back out several levels in a folder hierarchy and then descend back in. Hard-coding this directory path feels wrong, and I don't know even know if it's possible to restore a pyenv automatically from Visual Studio 2019.

I've read the other answers on StackOverflow regarding IronPython, and none of them have solved my problem. I want to call into python, from c#, and use an external library from pypi, with as much of that as possible being automatically restored by visual studio when another dev clones the repo.

Short of re-writing the whole application to use Python at the top level instead of C#, I don't know what to do in this situation. Can someone set me straight?

Okay, so here's what I ended up doing.

First, use PythonNET instead of IronPython if you need access to C extensions like Regex. IronPython doesn't play nicely with extensions, but PythonNET does.

Second, in order to reference your Python script from your C# project, you have to link the source file directly. The easiest way to do this in Visual Studio is to go to Solution Explorer > right-click on the target Project > Add > Existing Item. Select your .py file in the file selector, and make sure to change the mode from Add to Add As Link. Once you've done that, make sure to select the newly linked file in Solution Explorer, and go to Properties > Copy to Output Directory > Copy if newer. Now you can edit your Python script from its own dedicated project with full syntax highlighting support, and it'll be copied to the main project on build.

Unfortunately, this only really works for importing a single script file into your project. I think you could probably use similar steps to import an entire directory of scripts, but ultimately I ended up just dropping the .py file right in the project I was working on rather than treat it as a separate project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM