# Call Python Script with command line arguments within .dat file

**URL:** <https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889>\
**Category:** 3DEC\
**Tags:** python\
**Created:** [January 16, 2023, 9:48am UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889 "2023-01-16T09:48:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![yopiprabowo](https://avatars.discourse-cdn.com/v4/letter/y/77aa72/32.png) [@yopiprabowo](https://forum.itascainternational.com/u/yopiprabowo)\
**Post date:** [January 16, 2023, 9:48am UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/1 "2023-01-16T09:48:07Z")

</div>

Hi,

Is there anyway for us to call a python script within a dat file that includes a command line argument?

I have a python script that requires a command line argument and I want to iteratively call this script so I used the `program call` command but it does not recognize command line arguments so is there any alternative solution to that?

Thank you for your time.

---

<div class="post-metadata">

**Author:** ![Rima](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@Rima](https://forum.itascainternational.com/u/Rima)\
**Post date:** [January 16, 2023, 1:21pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/2 "2023-01-16T13:21:46Z")

</div>

Hi,  
I’m not sure to understand what you need to do exactly. Call a python script from a classical data file ? Can you please clarify ?  
Thanks,

Rima

---

<div class="post-metadata">

**Author:** ![Zakaria](https://avatars.discourse-cdn.com/v4/letter/z/b5ac83/32.png) [@Zakaria](https://forum.itascainternational.com/u/Zakaria)\
**Post date:** [January 16, 2023, 2:16pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/3 "2023-01-16T14:16:14Z")

</div>

Hello,  
It is possible to call a python file using the program call command (ex : **program call** “py\_script.py”) but you need to establish the connection between python and fish arguments. To do so you might use :  
python\_argument = **itasca.fish.get** (“fish\_argument”).  
Hope it helps you.

---

<div class="post-metadata">

**Author:** ![Theophile](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.itascainternational.com/theophile/32/299_2.png) [@Theophile](https://forum.itascainternational.com/u/Theophile)\
**Post date:** [January 23, 2023, 3:51pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/4 "2023-01-23T15:51:19Z")

</div>

Hi @yopiprabowo !

I agree with @Rima and @Zakaria: further explications would be great.  
An exemple file would be greater, even a very simplified one 😉 .

Regards.

Théophile

---

<div class="post-metadata">

**Author:** ![yopiprabowo](https://avatars.discourse-cdn.com/v4/letter/y/77aa72/32.png) [@yopiprabowo](https://forum.itascainternational.com/u/yopiprabowo)\
**Post date:** [January 23, 2023, 5:10pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/5 "2023-01-23T17:10:52Z")

</div>

Dear @Rima , @Zakaria , and @Theophile ,

Thank you for your responses. I figured out a way to do this in python without having to resort to FISH variables.

Let’s say i have a main function in a script (solve.py) where model solve command is called:

```auto
def main():
    #Apply gravity load and solve to equilibrium
    num_inc = 10 #set total number of increments
    inc_size = 0.1 # 0.1 x 10 = 1.0 (the full application of loads)
    p_1 = 0.0
    p_2 = 0.0
    for count in range(0,num_inc,1):
        mult = inc_size
        p1,p2 = main_loop_v2.main(mult,count) #Call the function that needs two arguments, where loads were incrementally applied
        p_1 += p1
        p_2 += p2
        print("solving for p1 : {} N/m2 and p2 : {} N/m2".format(p_1,p_2))
        dmax,vmax = getmaxdisp()
        it.fish.set("maxdisp",dmax)
        it.command("model solve mech ratio-local 1e-4")
        it.command("model save")

```

Notice that I called main\_loop\_v2.py where the main function requires two arguments from  
solve.py.

Thank you all for the help.

Regards,

YP

---

<div class="post-metadata">

**Author:** ![Theophile](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.itascainternational.com/theophile/32/299_2.png) [@Theophile](https://forum.itascainternational.com/u/Theophile)\
**Post date:** [February 1, 2023, 10:14am UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/6 "2023-02-01T10:14:04Z")

</div>

Hi @yopiprabowo !

First of all : sorry for the late response, I was away for a while.

So, I would indeed recommand driving directly from Python since you can easily run Fish commands from there and get access to 3DEC internal variables.

In my opinion, your script is a good solution.

By the way, the `range(n)` natively iterates from 0 to n with a step of 1, you do not need to provide `range(0,n,1)` 😉 .

```bash
>>> num_inc = 5
>>> for i in range(num_inc): print(i)
0
1
2
3
4
>>> for i in range(0, num_inc, 1): print(i)
0
1
2
3
4

```

Regards!

Théophile

---

<div class="post-metadata">

**Author:** ![yopiprabowo](https://avatars.discourse-cdn.com/v4/letter/y/77aa72/32.png) [@yopiprabowo](https://forum.itascainternational.com/u/yopiprabowo)\
**Post date:** [February 1, 2023, 10:28am UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/7 "2023-02-01T10:28:09Z")

</div>

Dear Théophile,

Thank you for your reply and your suggestion! Indeed typing `range(n)` instead of `range(0,n,1)` is a good practice! 😄

---

<div class="post-metadata">

**Author:** ![Newuser](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@Newuser](https://forum.itascainternational.com/u/Newuser)\
**Post date:** [February 26, 2024, 9:13pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/8 "2024-02-26T21:13:00Z")

</div>

Would you kindly explain to me on how does the "Program Call’ function works? I tried to solve the same data file provided in the FLAC 3D example problems but it shows error in the same line where “Program call” is provided. I am not able to see the link of the program call anywhere. Please see the attached photo.

 ![Screenshot 2024-02-26 151243](https://canada1.discourse-cdn.com/flex031/uploads/itasca/original/1X/d3457d6b75c139effd38407698aebcd8ec063c4e.png)

---

<div class="post-metadata">

**Author:** ![yopiprabowo](https://avatars.discourse-cdn.com/v4/letter/y/77aa72/32.png) [@yopiprabowo](https://forum.itascainternational.com/u/yopiprabowo)\
**Post date:** [February 27, 2024, 8:37am UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/9 "2024-02-27T08:37:40Z")

</div>

Hi!

What was the error message shown in the Console pane again?

If the message were something along the lines of “File not found”, I would suggest checking the location where the project file for this example is saved. Please see the snapshot below.

![image](https://canada1.discourse-cdn.com/flex031/uploads/itasca/original/1X/8a2c0b2b54f6b7e891d6391371ff0f0429c7a965.png)

Because I’m using 3DEC, the directory on my computer is:  
C:\Program Files\Itasca\3DEC700\DataFiles3D\FLAC\ExampleApplications\EmbankmentLoad

Yours would probably be:  
C:\Program Files\Itasca\FLAC3D700\DataFiles3D\FLAC\ExampleApplications\EmbankmentLoad

---

<div class="post-metadata">

**Author:** ![Newuser](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@Newuser](https://forum.itascainternational.com/u/Newuser)\
**Post date:** [February 27, 2024, 3:38pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/10 "2024-02-27T15:38:04Z")

</div>

Thanks. But my error is like this. what could be the reason for this? I am running the same data file of the built in example but yet it is not running.

 ![Screenshot 2024-02-27 093617](https://canada1.discourse-cdn.com/flex031/uploads/itasca/original/1X/21d9c4844aa317808827a2398c8ed5030d77e4da.png)

---

<div class="post-metadata">

**Author:** ![Newuser](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@Newuser](https://forum.itascainternational.com/u/Newuser)\
**Post date:** [February 27, 2024, 3:51pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/11 "2024-02-27T15:51:53Z")

</div>

Whenever I apply ‘program call’ for other task as well, error would still be the same. (i.e. error opening file).

---

<div class="post-metadata">

**Author:** ![yopiprabowo](https://avatars.discourse-cdn.com/v4/letter/y/77aa72/32.png) [@yopiprabowo](https://forum.itascainternational.com/u/yopiprabowo)\
**Post date:** [February 27, 2024, 3:58pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/12 "2024-02-27T15:58:26Z")

</div>

Hi. Would you mind taking a screenshot of what the directory looks like in  
C:\Users\samee\OneDrive\Documents\Itasca\flac3d900\My Projects\

Thanks

---

<div class="post-metadata">

**Author:** ![Newuser](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@Newuser](https://forum.itascainternational.com/u/Newuser)\
**Post date:** [February 27, 2024, 4:00pm UTC](https://forum.itascainternational.com/t/call-python-script-with-command-line-arguments-within-dat-file/889/13 "2024-02-27T16:00:48Z")

</div>

I solved the issue. Thank you very much.
