Revit could not complete the external command

I am trying to export simple data from some of my scripts in CSV format. I added a simple test Python code to both Python and Dynamo test tools, which export the CSV. It works without any problem for the Python tool. However, when I first ran the Dynamo tool with pyRevit, running the Python tool afterward triggers the error “Revit could not complete the external command.” Am I missing something here?
This Is the code I used in both dyn and py files:
import csv
import datetime

data = [
[“Name”, “Age”, “City”],
[“Alice”, 30, “New York”],
[“Bob”, 25, “Los Angeles”],
[“Charlie”, 35, “Chicago”]
]

directory_path = r’P:\Operations\Drawing Resources\Revit\2024-All CSV files’

now = datetime.datetime.now()
timestamp = now.strftime(“%Y-%m-%d_%H-%M-%S”)

X = ‘example_variable’

file_name = f’{timestamp}_{X}.csv’
file_path = f’{directory_path}\{file_name}’

with open(file_path, mode=‘w’, newline=‘’) as file:
writer = csv.writer(file)
writer.writerows(data)

image

1 Like

As you are using cpython you should check @ErikFrits video about importing modules:

Or as mentioned in

CPython engine is under active development and might be unstable. If you want to use python for development, it is preferred to use IronPython. CPython should only be used when accessing C-based python packages (e.g. numpy, scipy) is necessary.

1 Like

Thank you for your reply. Converting to IronPython solved the issue!

Now python 3.8.5 version isn’t longer supported. How could I understand what Py version is needed for another packages. And if PyRevit isn’t supported with CPython, how may I addapte my scripts on IronPython for Revit 2021-2022 to Revit 2025. I will realy thank if you help me or somehow direct my thought in the right direction)
P.S. from the specific packages I use Microsoft.Office.Interop.Excel and pyrevit.forms.WPFWindow

xlsxwriter and xlrd is shipped with pyrevit - have you considered using that?
xlrd is old enough to still support xlsx, later support was limited to xls. Chatgpt sample:

# -*- coding: utf-8 -*-
from pyrevit import script
import os

# Import libraries
import xlsxwriter
import xlrd

output = script.get_output()

# Define file path (inside user's temp directory)
file_path = os.path.join(os.environ['TEMP'], 'pyrevit_excel_demo.xlsx')

# ---------------------------
# WRITE EXCEL (XlsxWriter)
# ---------------------------
workbook = xlsxwriter.Workbook(file_path)
worksheet = workbook.add_worksheet("Demo")

# Write some sample data
data = [
    ["Element ID", "Name", "Value"],
    [1, "Wall", 12.5],
    [2, "Door", 3.2],
    [3, "Window", 5.8],
]

for row_idx, row in enumerate(data):
    for col_idx, value in enumerate(row):
        worksheet.write(row_idx, col_idx, value)

workbook.close()

output.print_md("### Excel file written to: `{}`".format(file_path))


# ---------------------------
# READ EXCEL (xlrd)
# ---------------------------
book = xlrd.open_workbook(file_path)
sheet = book.sheet_by_index(0)

output.print_md("### Reading back data:")

for row_idx in range(sheet.nrows):
    row_values = sheet.row_values(row_idx)
    output.print_md("- {}".format(row_values))

1 Like

No, while I’m trying to launch WPF window