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.

3
  • 1
    Possible duplicate of Python: Run function from the command line Commented May 5, 2017 at 9:54
  • 1
    i want to run a script from a python interpreter not OS command line Commented May 5, 2017 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. Commented May 5, 2017 at 14:37

1 Answer 1

2
>>> import subprocess
>>> subprocess.run('python script.py input_data.csv', shell=True)
Sign up to request clarification or add additional context in comments.

2 Comments

i am liking this Claudio, have not seen this elsewhere
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.