PTVS icon indicating copy to clipboard operation
PTVS copied to clipboard

Python project reports a .NET Framwork error when .NET Core library is referenced

Open vsfeedback opened this issue 2 years ago • 5 comments

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice] Python project reports a problem (when it references a .NET Core library project): "Project '.. \ClassLibrary\ClassLibrary.csproj' targets 'net6.0'. It cannot be referenced by a project that targets '. NETFramework,Version=v4.7.2'. PythonExample C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 1830"

Steps to reproduce: Create a .NET Core library and reference it by Python project.

The Python interpreter has pythonnet package installed and loads and executes .NET Core library successfully. Just the error is without meaning as Python projects use Python interpreter and not .NET framework.

The example project illustrating the error is attached.


Original Comments

Feedback Bot on 2/15/2023, 10:13 PM:

(private comment, text removed)


Original Solutions

(no solutions)

vsfeedback avatar Feb 22 '23 19:02 vsfeedback

PythonExample.zip

This is the example code that produces the error.

rajko-horvat avatar Feb 23 '23 09:02 rajko-horvat

Python projects pretend to be .NET Framework projects to integrate with VS web tooling. This is the unfortunate side effect.

That said, references from Python projects to .NET ones are generally of limited use; I'm not even sure we set up the search path properly in this case. Is there any particular functionality that you expect to become available if the reference worked?

int19h avatar Feb 27 '23 19:02 int19h

Hi, It's other way around. I'm referencing .NET core library project in Python console project because Python uses .NET core library. In that context it should not matter what type of .NET project(s) are referenced in Python project as long as Python code uses them properly, and the aforementioned error should not be generated at all.

Also, the generated binary .NET library file is in a proper place for Python code to use it.

I hope that clears the things a little.

rajko-horvat avatar Feb 28 '23 12:02 rajko-horvat

That's the kind of reference I was referring to, as well: a Python project referencing a non-Python one. This really only does one thing: it adds the output directory of the other project to the Python project search paths. This feature was originally designed with C++ DLL projects in mind, though, so I'm not even sure that it works correctly for .NET project.

For now, I think, this is just not a supported scenario for this feature. The workaround is to manually add a (relative) search path to point at the output of the .NET project for the same effect.

If we do decide to implement this, we'd need to rework all our .targets files to build on top of .NET Core .targets instead of .NET Framework ones. This might be non-trivial when it comes to wiring up web tooling - we use a lot of ASP.NET WebForms machinery, but it's not supported in .NET Core.

int19h avatar Mar 02 '23 06:03 int19h

It works correctly as it outputs the .NET core library in Python directory successfully. Well, I had to rework our ASP.NET WebForms and code to .NET core and it is not trivial, but once you get a sense of it it goes smoothly ;) I have to admit, some things in .NET core are so much easier to implement than in ASP.NET.

It would be a nice to implement these things in the future :)

rajko-horvat avatar Mar 02 '23 10:03 rajko-horvat