Planning Analytics

Planning Analytics

Get AI-infused integrated business planning

 View Only
Expand all | Collapse all

Executecommand TM1py scritpt

  • 1.  Executecommand TM1py scritpt

    Posted Fri July 08, 2022 06:04 AM
    Edited by System Admin Fri January 20, 2023 04:22 PM
      |   view attached

    Hello,

    I want to trigger a python script using the EXECUTECOMMAND function. The python script is triggered by a .bat file. 
    The issue is that the TM1 process keeps running (not executed).

    I've tested the following:

    • When directly executed in CMD it works. (See screenshot)
    • Executing the bat from TM1 file works if I run a different command. Ex. a command that moves a file, move "C:\Program Files\Python39\Dimensions.xlsx" "C:\Junyper Notebooks\FNV".
    • The location of the python.exe file is accessible to the executing user (local admin).

    Info:
    Command line in bat file: "C:\Program Files\Python39\python.exe" "C:\Junyper Notebooks\FNV\LoadToExcelScript.py"
    TM1 line: EXECUTECOMMAND('C:\Junyper Notebooks\FNV\Run script.bat',1);

    Anny suggestions?

    ------------------------------
    Felix Stuyck
    ------------------------------
    #PlanningAnalyticswithWatson


  • 2.  RE: Executecommand TM1py scritpt

    Posted Fri July 08, 2022 06:39 AM
    HI Felix,

    Two things:
    1) Your ExecuteCommand has a Wait argument of 1 telling TI to wait until the call has completed. If you want to trigger the python script and let it run in the background, try set the Wait to 0
    2) I typically execute batch files using CMD /C <batch> e.g. EXECUTECOMMAND('CMD /C C:\Junyper Notebooks\FNV\Run script.bat',1);
    This tells the command prompt to execute and not to wait.

    Hope that helps.

    ------------------------------
    George Tonkin
    ------------------------------



  • 3.  RE: Executecommand TM1py scritpt

    Posted Fri July 08, 2022 07:54 AM

    Hi George,

    Thanks for the response.
    1) I actualy want the process to wait, so I get an indication when it is finished. 
    2) I've tested your suggestion, but this does not change anything.
        EXECUTECOMMAND('CMD /C "C:\Junyper Notebooks\FNV\Run script.bat"',1);



    ------------------------------
    Felix Stuyck
    ------------------------------



  • 4.  RE: Executecommand TM1py scritpt

    Posted Fri July 08, 2022 09:09 AM
    May be worthwhile adding some more code to the batch file to echo your environment to a text file and possibly use 2>error.txt on the command executing the python script to see if there is an error being generated.

    ------------------------------
    George Tonkin
    ------------------------------



  • 5.  RE: Executecommand TM1py scritpt

    Posted Fri July 08, 2022 11:04 AM
    Edited by System Admin Fri January 20, 2023 04:26 PM
      |   view attached

    I've tried your suggestion and witnessed the following behaviour.

    The bat code is updated to:
    cd C:\Junyper Notebooks\FNV\Scripts
    for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I break>errors.err
    "C:\Program Files\Python39\python.exe" "C:\Junyper Notebooks\FNV\Scripts\LoadToExcelScript.py" > .\logs\log_%datetime:~0,8%-%datetime:~8,6%.log 2>> errors.err

    When run from CMD this generates a log file with the output of the CMD (screenshot).
    When run from TM1 this generates and empty log file. To me, this means that the .py file is still not triggerd.

    The TM1 process does return success, but this is not true. It is probably because the first line in the bat file was executed successfully.



    ------------------------------
    Felix Stuyck
    ------------------------------



  • 6.  RE: Executecommand TM1py scritpt

    Posted Fri July 08, 2022 12:11 PM
    HI Felix,

    That Break concerns me a bit and wondering if it somehow may be executing and exiting the batch file before running the python script.
    Should not do so if running from cmd works but one never knows without stepping through the code.

    To try and skip the loops etc. could you try something like:
    set myTimeTemp=%time: =0%
    set myTime=%date:~0,4%%date:~5,2%%date:~8,2%-%myTimeTemp:~0,2%%myTimeTemp:~3,2%
    
    cd "C:\Junyper Notebooks\FNV\Scripts"
    
    "C:\Program Files\Python39\python.exe" "C:\Junyper Notebooks\FNV\Scripts\LoadToExcelScript.py" > .\logs\log_%myTime%.log 2>>errors.err ​


    ------------------------------
    George Tonkin
    ------------------------------



  • 7.  RE: Executecommand TM1py scritpt

    Posted Mon July 11, 2022 02:14 AM
    Hello Felix,

    Just a couple questions to put aside the obvious as I see you do a "cd C:\something" :
    - is your tm1 data directory on C: drive ? 
    - when you run the batch file from the command line, do you run it from your tm1 data directory ?

    If it is on another drive you'll need to add a "C:" command before the "cd" in your batch file, cd does not change drive...

    For the timestamp I would simply do a TIMST in TM1 then build the the batch file with AsciiOutput commands, I don't like standalone scripts... and I also like simplicity, this dos command is just too complex to handle for anyone that is not fluent in command line code :)

    Cheers,

    ------------------------------
    Laurent Henssien
    C L A R I T Y
    https://www.clarity.consulting
    ------------------------------



  • 8.  RE: Executecommand TM1py scritpt

    Posted Mon July 11, 2022 08:17 AM

    Felix:

    So you are saying that the BAT works using your ID logged into the server machine but the BAT does not work running under the user associated with the TM1 service (LOCAL ADMIN).

    - I agree with Laurent's suggestion on the C: drive.  Though it looks like the Python call is fully qualified with <drive>\<path> for all the files referenced.

    - Have you tried using a BAT file to capture the environment variables associated with the Local Admin account.  My assumption is that the environment variables of your user ID do not match that of the Local Admin and Python cannot launch properly.

    - You could also associate the TM1 Server service call with your user ID temporarily to see if that works but that assumes your ID has the proper rights to invoke the TM1 Server etc..



    ------------------------------
    Daniel Bernatchez
    Clear Apex LLC
    ------------------------------



  • 9.  RE: Executecommand TM1py scritpt

    Posted Mon July 11, 2022 09:47 AM
    I would also suggest embedding all of the command line logic into the python script. That way you have access to things like robust logging, exception handling, and a testing framework. Complex ExecuteCommands are really hard to troubleshoot and debug.

    ------------------------------
    Ryan Clapp
    ------------------------------



  • 10.  RE: Executecommand TM1py scritpt

    Posted Mon July 11, 2022 03:11 PM
    Is your tm1 service running under Local System user? In my setup "Local System" had not enough privileges to call the python exe from c drive, so we replaced it with a real admin account and it worked.

    ------------------------------
    Luiz gustavo Ribeiro
    ------------------------------



  • 11.  RE: Executecommand TM1py scritpt

    Posted Tue July 12, 2022 04:57 AM
    Edited by System Admin Fri January 20, 2023 04:49 PM

    Thank you for all the suggestions, I've found the cause.

    I've tested a more simple python script and that worked, meaning the issue was in the script I tried to run.
    The cause was twofold:

    • The keyring was not working, probably user related.
      "password = keyring.get_password(INSTANCE, user)".
    • Importing functions defined in a separate script was not working ("import fin_functions as fv").
      I do not fully understand the cause, but when I explicitly add this location to the environment variables it works.
      "import sys
      sys.path.append('C:/Junyper Notebooks/FNV')"


    ------------------------------
    Felix Stuyck
    ------------------------------