5

I am using PyZo(with python3.5) and dont know how to run a script with arguments from PyZo's python interpreter - or from python interpreter in general. I found following working example here for python3 but dont know how to pass arguments (e.g. csv file input_data.csv) to the script

>>> exec(open("./script.py").read())

This is working in iPython:

In [1]: run script.py input_data.csv

What is the python 3 equivalent of the iPython command above ?

Thanks


Note 1

When running a script with arguments from an OS command line you type this:

$ python script.py input_data.csv

What I would expect when using python interpreter is being able to run a python script e.g. like this:

>>> script.py input_data.csv

i.e. without calling python executable, or using 'exec(open("./script.py").read())', etc. For me running a script with arguments is very fundamental thing to do but apparently not for majority of users.

Community
  • 1
  • 1
francek
  • 492
  • 4
  • 11
  • 1
    Possible duplicate of [Python: Run function from the command line](http://stackoverflow.com/questions/3987041/python-run-function-from-the-command-line) – Sergey Shubin May 05 '17 at 09:54
  • 1
    i want to run a script from a python interpreter not OS command line – francek May 05 '17 at 10:05
  • 3
    The "python3 equivalent" is either (i) to install IPython for Python 3 and just use it, or (ii) (somewhat more clunkily) set `sys.argv=['script.py', 'input_data.csv']` by hand before executing the content of `script.py`. But I think what you really want is the ***PyZo*** equivalent of IPython's `run`. I wouldn't be surprised to learn that that exists in some form but it's a PyZo-specific question. – jez May 05 '17 at 14:37

1 Answers1

2
>>> import subprocess
>>> subprocess.run('python script.py input_data.csv', shell=True)
Claudio
  • 7,474
  • 3
  • 18
  • 48
  • i am liking this Claudio, have not seen this elsewhere – francek May 09 '17 at 19:20
  • Sounds like a nice idea, but it fails in what I see as a key benefit of running something from interpreter, the ability to further access/experiment with the variables afterwards, as this is spawning a separate process. – JeopardyTempest Feb 18 '22 at 04:56