<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Error C# Excuted Python file. in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12329092#M9025</link>
    <description>&lt;P&gt;Hi everyone!&lt;BR /&gt;I tried using C# run python file but when click to button on ribbon Revit show this error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;"Revit file or Assembly Microsoft.Scripting, Version = 1.3.4.0,, Culture = neutral, PuplicKeyToken = 7f709c5b713576e1, or can not read file. File path not find...&lt;BR /&gt;&lt;BR /&gt;But that error will not show if I click another botton created Dynamo file (.dyn) first then comback click it.&lt;/P&gt;&lt;P&gt;1. The code run Python file by below:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;public class Check : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // Define the file path to your Python script
            string pythonScriptFilePath = @"C:\Test\Revit\Tool\script.py";

            UIApplication _revit = commandData.Application;
            UIDocument uidoc = _revit.ActiveUIDocument;
            Application app = _revit.Application;
            Document doc = uidoc.Document;

            var flags = new Dictionary&amp;lt;string, object&amp;gt;() { { "Frames", true }, { "FullFrames", true } };

            var py = IronPython.Hosting.Python.CreateEngine(flags);
            var scope = IronPython.Hosting.Python.CreateModule(py, "__main__");

            ScriptRuntime runtime = IronPython.Hosting.Python.CreateRuntime();
            ScriptEngine engine = runtime.GetEngine("py");

            scope.SetVariable("__commandData__", commandData);

            // Add special variable: __revit__ to be globally visible everywhere:
            var builtin = IronPython.Hosting.Python.GetBuiltinModule(py);
            builtin.SetVariable("__revit__", _revit);
            py.Runtime.LoadAssembly(typeof(Autodesk.Revit.DB.Document).Assembly);
            py.Runtime.LoadAssembly(typeof(Autodesk.Revit.UI.TaskDialog).Assembly);

            try
            {
                py.ExecuteFile(pythonScriptFilePath);
            }
            catch (Exception ex)
            {
                TaskDialog myDialog = new TaskDialog("IronPython Error");
                myDialog.MainInstruction = $"Couldn't execute IronPython script {pythonScriptFilePath}: ";
                myDialog.ExpandedContent = ex.Message;
                myDialog.Show();
            }

            return Result.Succeeded;
        }
    }&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 25 Oct 2023 06:50:20 GMT</pubDate>
    <dc:creator>manhgt214</dc:creator>
    <dc:date>2023-10-25T06:50:20Z</dc:date>
    <item>
      <title>Error C# Excuted Python file.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12329092#M9025</link>
      <description>&lt;P&gt;Hi everyone!&lt;BR /&gt;I tried using C# run python file but when click to button on ribbon Revit show this error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;"Revit file or Assembly Microsoft.Scripting, Version = 1.3.4.0,, Culture = neutral, PuplicKeyToken = 7f709c5b713576e1, or can not read file. File path not find...&lt;BR /&gt;&lt;BR /&gt;But that error will not show if I click another botton created Dynamo file (.dyn) first then comback click it.&lt;/P&gt;&lt;P&gt;1. The code run Python file by below:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;public class Check : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // Define the file path to your Python script
            string pythonScriptFilePath = @"C:\Test\Revit\Tool\script.py";

            UIApplication _revit = commandData.Application;
            UIDocument uidoc = _revit.ActiveUIDocument;
            Application app = _revit.Application;
            Document doc = uidoc.Document;

            var flags = new Dictionary&amp;lt;string, object&amp;gt;() { { "Frames", true }, { "FullFrames", true } };

            var py = IronPython.Hosting.Python.CreateEngine(flags);
            var scope = IronPython.Hosting.Python.CreateModule(py, "__main__");

            ScriptRuntime runtime = IronPython.Hosting.Python.CreateRuntime();
            ScriptEngine engine = runtime.GetEngine("py");

            scope.SetVariable("__commandData__", commandData);

            // Add special variable: __revit__ to be globally visible everywhere:
            var builtin = IronPython.Hosting.Python.GetBuiltinModule(py);
            builtin.SetVariable("__revit__", _revit);
            py.Runtime.LoadAssembly(typeof(Autodesk.Revit.DB.Document).Assembly);
            py.Runtime.LoadAssembly(typeof(Autodesk.Revit.UI.TaskDialog).Assembly);

            try
            {
                py.ExecuteFile(pythonScriptFilePath);
            }
            catch (Exception ex)
            {
                TaskDialog myDialog = new TaskDialog("IronPython Error");
                myDialog.MainInstruction = $"Couldn't execute IronPython script {pythonScriptFilePath}: ";
                myDialog.ExpandedContent = ex.Message;
                myDialog.Show();
            }

            return Result.Succeeded;
        }
    }&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 25 Oct 2023 06:50:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12329092#M9025</guid>
      <dc:creator>manhgt214</dc:creator>
      <dc:date>2023-10-25T06:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Error C# Excuted Python file.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12329134#M9026</link>
      <description>&lt;P&gt;So, it seems that a required assembly cannot be found and loaded. You can either load it yourself specifically up front or implement an assembly resolver to specify more exactly where it can be found in case of need. Since clicking the other button first helps, it obviously &amp;nbsp;exists somewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 07:09:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12329134#M9026</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2023-10-25T07:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: Error C# Excuted Python file.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12329153#M9027</link>
      <description>&lt;P&gt;thank you Sir, but implement dynamo seem like same by run path.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
Document doc = uiapp.ActiveUIDocument.Document;

string Dynamo_Script_Path = @"C:\Revit\Tool\script.dyn";
DynamoRevit dynamoRevit = new DynamoRevit();

DynamoRevitCommandData dynamoRevitCommandData = new DynamoRevitCommandData();
dynamoRevitCommandData.Application = uiapp;
IDictionary&amp;lt;string, string&amp;gt; journalData = new Dictionary&amp;lt;string, string&amp;gt;
{
{ Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime (run in background)
{ Dynamo.Applications.JournalKeys.AutomationModeKey, false.ToString() }, //run journal automatically (tested: trus is not run)
{ Dynamo.Applications.JournalKeys.DynPathKey, Dynamo_Script_Path }, //run node at this file path
{ Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() },
// The journal file can specify if the Dynamo workspace opened from DynPathKey will be executed or not.
//If we are in automation mode the workspace will be executed regardless of this key.
{ Dynamo.Applications.JournalKeys.ForceManualRunKey, true.ToString() }, //set true to run in manual mode
{ Dynamo.Applications.JournalKeys.ModelShutDownKey, false.ToString() }, //increase speed when click to button on ribbon
{ Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }
};

dynamoRevitCommandData.JournalData = journalData;
Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);
return externalCommandResult;
}&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 25 Oct 2023 07:18:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12329153#M9027</guid>
      <dc:creator>manhgt214</dc:creator>
      <dc:date>2023-10-25T07:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: Error C# Excuted Python file.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12330830#M9028</link>
      <description>&lt;P&gt;In Revit 2021 and 2022, Dynamo uses&amp;nbsp;&lt;SPAN&gt;Microsoft.Scripting version 1.2.2.1000. You should probably reference the same version in your add-in if you use one of those versions. If it only works if you run Dynamo first, then you need to load the assembly yourself, maybe with an assembly resolver or maybe just place it in the same directory as your add-in. If you load a different version of the assembly than what Dynamo uses, it could cause problems for Dynamo.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In 2023 and 2024, Dynamo does not use Microsoft.Scripting. For these versions, you should probably try to use the libraries that Dynamo uses though you might be able to use Microsoft.Scripting if you include it with your add-in.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 18:38:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/error-c-excuted-python-file/m-p/12330830#M9028</guid>
      <dc:creator>mhannonQ65N2</dc:creator>
      <dc:date>2023-10-25T18:38:53Z</dc:date>
    </item>
  </channel>
</rss>

