Python Invoke Method error

import calendar
from datetime import datetime

def get_valid_dates(varcount):
# Get the current year and month
now = datetime.now()
year = now.year
month = now.month + 3

# Adjust year if month exceeds December
if month > 12:
    year += 1
    month -= 12

# Get the calendar matrix for the specified month
month_calendar = calendar.monthcalendar(year, month)

# Store the valid dates in a list, excluding Saturdays, Sundays, Mondays, and specified dates
valid_dates = []

# Find the index of the first Monday in the month
first_monday_index = next((i for i, day in enumerate(month_calendar) if day[calendar.MONDAY] != 0), -1)

if first_monday_index == -1:
    return valid_dates  # No complete week starting from Monday

# Append the valid dates for the first three full weeks starting from Monday to the list
for week in month_calendar[first_monday_index:first_monday_index+3]:
    for day in week:
        if day != 0 and calendar.weekday(year, month, day) not in [0, 4, 5, 6]:  # Exclude Mondays, Fridays, Saturdays, and Sundays
            # Check if the date is not in the excluded list
            if not is_excluded_date(day, month):
                # Get the day of the week number (1 for Monday, 2 for Tuesday, etc.)
                day_of_week = calendar.weekday(year, month, day) + 1
                valid_dates.append(f"{day:02d}/{month:02d}/{year}/{day_of_week}")

# Count the number of valid dates
count = len(valid_dates)

# Calculate repetitions and remainder for varcount
repetitions = varcount // count
remainder = varcount % count

# Define a function to extract the last day from the date
def get_last_day(date_string):
    return int(date_string.split('/')[-1])

# Sort the valid dates based on the last day
valid_dates.sort(key=get_last_day)

# Prepare the list of dates to return
dates_to_return = []
for i in range(repetitions):
    dates_to_return.extend(valid_dates)

dates_to_return.extend(valid_dates[:remainder])

return dates_to_return

def is_excluded_date(day, month):
# List of excluded dates (day/month)
excluded_dates = [
(1, 1),
(6, 1),
(29,3),
(1,4),
(1,5),
(9,5),
(20,5),
(30,5),
(3,10),
(31,10),
(1,11),
(24,12),
(25, 12),
(26,12),
(27,12),
(28,12),
(29,12),
(30,12),
(31,12),
(1,1),
(2,1)
]
return (day, month) in excluded_dates

I am using this Python script in UiPath as follows,


But this is giving Invoke Python Method: One or more errors occurred. (Error invoking Python method) error. Help me to solve this error.

Hi @Athul_A_k, This error seems to be generic and it could be of various reasons. Can you ensure that you have Python installed on the machine where UiPath is running and that the Python environment is properly configured? Verify the Python version compatibility with UiPath.

Also, verify that there are no syntax errors or typos in your Python script. Even small errors can cause the script to fail.

@Athul_A_k ,

If the python script is correct and without error there could be possibility of misconfigured or incomplete python setup.

One of the prerequisite which most people miss is .Net runtime 5 - Download .NET 5.0 (Linux, macOS, and Windows)

Check this.

Thanks,
Ashok :slight_smile:

Same code is working fine when i am assigning varcount.

image