# Run a python script with command line arguments

**URL:** <https://discourse.slicer.org/t/run-a-python-script-with-command-line-arguments/36718>\
**Category:** Support\
**Tags:** python\
**Created:** [June 12, 2024, 11:25am UTC](https://discourse.slicer.org/t/run-a-python-script-with-command-line-arguments/36718 "2024-06-12T11:25:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RomanStriker](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@RomanStriker](https://discourse.slicer.org/u/RomanStriker)\
**Post date:** [June 12, 2024, 11:25am UTC](https://discourse.slicer.org/t/run-a-python-script-with-command-line-arguments/36718/1 "2024-06-12T11:25:34Z")

</div>

Hi,

I am trying to run a python script that accepts command line arguments as follows.

```auto
./Slicer --python-script /home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop

```

and the python script looks as follows

```auto
def extract_centerlines(args):
...

if __name__ == ' __main__':
    parser = argparse.ArgumentParser(description="Extract centerlines from a dataset")
    parser.add_argument('--data_dir', type=str, help='Path to the dataset directory')
    parser.add_argument('--end_points', action='store_true', default=False, help='Save the endpoints')
    parser.add_argument('--network', action='store_true', default=False, help='Save the network model')
    parser.add_argument('--network_curve', action='store_true', default=False, help='Save the network curve')
    parser.add_argument('--network_prop', action='store_true', default=False, help='Save the network properties')
    parser.add_argument('--centerline', action='store_true', default=False, help='Save the centerline model')
    parser.add_argument('--centerline_curve', action='store_true', default=False, help='Save the centerline curve')
    parser.add_argument('--centerline_prop', action='store_true', default=False, help='Save the centerline properties')
    args = parser.parse_args()
    
    extract_centerlines(args)

```

when I run this script using the command above, I get

```auto
./Slicer --python-script /home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop
>

```

guessing that it’s expecting something more. I also tried

```auto
./Slicer --python-script "/home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop"

```

but I get the error

```auto
./Slicer --no-splash --no-main-window --python-script "/home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop"

```

How to run this script with the arguments?

---

<div class="post-metadata">

**Author:** ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)\
**Post date:** [June 12, 2024, 11:31am UTC](https://discourse.slicer.org/t/run-a-python-script-with-command-line-arguments/36718/2 "2024-06-12T11:31:43Z")

</div>

@RomanStriker, check this post and see if it helps:

> [@Slicer parameters conflicts when adding "--load-file" and "--python-script"](https://discourse.slicer.org/t/slicer-parameters-conflicts-when-adding-load-file-and-python-script/33390):
>
> Slicer.exe, “–python-code”, “print(‘Start Slicer from Blender!’)”, “–python-script”, startup\_slice\_script\_file When adding both “–load-file” and “–python-script”, “–load-file” will not be executed to load .mrb file, and it only executes “–python-script”. Is there a way of adding both parameters and make it working? Thanks.

---

<div class="post-metadata">

**Author:** ![RomanStriker](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@RomanStriker](https://discourse.slicer.org/u/RomanStriker)\
**Post date:** [June 12, 2024, 12:07pm UTC](https://discourse.slicer.org/t/run-a-python-script-with-command-line-arguments/36718/3 "2024-06-12T12:07:35Z")

</div>

Thanks. I can use the sys.argv as follows

```bash
./Slicer --python-script /home/user/extract_centerlines_vmtk.py "/home/user/data" end_points network network_curve network_prop centerline centerline_curve centerline_prop

```

and in the script

```py
def extract_centerlines(args):
...

if __name__ == ' __main__':
    # create args object
    args = argparse.Namespace(
        dataset_dir=sys.argv[1],
        end_points=True if 'end_points' in sys.argv else False,
        network=True if 'network' in sys.argv else False,
        network_curve=True if 'network_curve' in sys.argv else False,
        network_prop=True if 'network_prop' in sys.argv else False,
        centerline=True if 'centerline' in sys.argv else False,
        centerline_curve=True if 'centerline_curve' in sys.argv else False,
        centerline_prop=True if 'centerline_prop' in sys.argv else False,
    )
    
    extract_centerlines(args)

```
