#18 clr.AddReference fails when assembly is built with .NET 4.0



I am not sure if it is a bug or an incompatibility between the CLR versions but here it is:

I can successfully load an assembly built with version 2.0, 3.0 and 3.5 but it fails to load with 4.0. I always get a file not found exception

File "<stdin>", line 1, in <module>
System.IO.FileNotFoundException: Unable to find assembly 'MyPythonAssemby'.
at Python.Runtime.CLRModule.AddReference(String name) in H:\Documents\Visual
Studio 2008\Projects\PySharp\trunk\pythonnet\src\runtime\moduleobject.cs:line 375

Does python for .NET support .NET 4.0?



  • Guillaume

    Guillaume - 2011-04-26


    After recompiling with vs2010 and targeting .NET 4.0 my assembly loads fine. So I closed the ticket.

  • Guillaume

    Guillaume - 2011-04-26
    • status: open --> closed
  • Barton Cline

    Barton Cline - 2011-05-03

    Here is how oleksii got VS2010 and .NET 4.0 working:

    1. Get the sources (tarball from sourceforge or directly from SVN)
    2. Open the pythonnet.sln with VS2010 and convert to 2010 format (will happen automagically)
    3. Change the target framework to 4. Follow the following step for EACH project
    3.1. Right-click on the project name and select "Properties"
    3.2. Select the "Application" tab on the left (if not selected yet)
    3.3. Change the "Target framework" to ".NET Framework 4"
    4. Open the clrmodule.il and change the lines with the version number in the following piece of code

    .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 2:0:0:0 }

    to have

    .ver 4:0:0:0

    5. Open the buildclrmodule.bat and change the

    %windir%\Microsoft.NET\Framework\v2.0.50727\ilasm /nologo /quiet /dll %ILASM_EXTRA_ARGS% /include=%INCLUDE_PATH% /output=%OUTPUT_PATH% %INPUT_PATH%


    %windir%\Microsoft.NET\Framework\v4.0.30319\ilasm /nologo /quiet /dll %ILASM_EXTRA_ARGS% /include=%INCLUDE_PATH% /output=%OUTPUT_PATH% %INPUT_PATH%

    5. Recompile the whole solution, ignore the deprecation warnings.

    Now you have all necessary files under the pythonnet folder where you have the sources. You need clr.pyd, python.exe and Python.Runtime.dll.

  • Sach

    Sach - 2012-05-28


    Although it looks like an old post but I ran into a similar issue. Tried the steps mentioned by barton_c. Unfortunately, I'm getting error- "import clr- The module is not initialized properly".

    To give a brief background, I'm trying to do a drag and drop automation using Python. I've successfully use UIAutomationClient.dll and UIAutomationTypes.dll in the past. To do drag and drop I tried importing Microsoft.VisualStudio.TestTools.UITesting.dll using clr provided by Microsoft but ran out of luck!

    Any help/solution is eagerly awaited and much appreciated.



Log in to post a comment.