0

I am using Paraview's Python Shell and need to run a script with parameters. I've already tried to run a script without parameters using: exec(open('/home/otamichalek/Documents/Scritps/V1/OpenFile-V1-sample.py').read())

then I created V2 of the script, which instead of exact definition of filename has filename = str(sys.argv[1])

I need to input multiple arguments, but I wanted to try it using just one first. I need something like exec(open('/home/otamichalek/Documents/Scritps/V1/OpenFile-V1-sample.py' **'FileToOpenWithData'**).read())

Appreciate any help

  • Why are you using exec to open a file? – Ronald Jun 30 '20 at 10:29
  • Simple open did not work for me. Should I use something else? – Ota Michálek Jul 01 '20 at 07:29
  • Now I have tried ```import sys``` then ```sys.argv=['data file']``` and then ```execfile(/home/otamichalek/Documents/Scritps/OpenFile-V2-short.py)``` and I am getting ``` Traceback (most recent call last):``` ``` File "/usr/lib/python3.8/code.py", line 90, in runcode``` ``` exec(code, self.locals)``` ``` File "", line 1, in ``` ```TypeError: 'str' object is not callable``` – Ota Michálek Jul 01 '20 at 07:31
  • If you dont really need the GUI, you can use pvpython / pvbatch to run ParaView Script. Otherwise, I think you'd better create a module with a method taking a file list as arguments – Nico Vuaille Jul 01 '20 at 08:08
  • I needed the GUI to check that it does what I want it to. Thank you – Ota Michálek Jul 01 '20 at 08:25

1 Answers1

1

Got it to work:

import runpy, sys

#argv[0] will be replaced by runpy

sys.argv = ['', 'datafile']
runpy.run_path('script.py')

from: https://stackoverflow.com/a/33259769/13840062