1

When you run "python" in your terminal, you get a running instance of python where you can run python code.

I want to do this, but after my script has run a few functions first.

enter image description here

But what I want is this type of interface at the end of my script.

enter image description here

I.e. I want to run my_script.py, in "main" call a few functions, but then after those functions, keep the "workspace" open with the >>> interface so that the user can run more python code.

if __name__ == "__main__":

    excel_file = sys.argv[1]
    my_list = load_file(excel_file) 
    #... do a bunch of other things

    # open python interface now >>>
    while True:
        # accept user python commands like in the terminal example above

Is there any way to do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
JDS
  • 16,388
  • 47
  • 161
  • 224
  • 1
    could you not just define a function that accepts user input (i.e. with input in python 3) and then uses those inputs to do whatever you want it to do? Or you're looking for a truly interactive python shell? – brobertsp Jun 29 '21 at 19:30
  • I'd try that out. Basically I want to create a makeshift "workspace", where first I load files into variables, and then I play around with the variables with a python shell (e.g. make graphs, save outputs, etc). Think "poor man's matlab" if you're familiar lol. I would try your solution, want to put up an answer? – JDS Jun 29 '21 at 19:33
  • I am happy to write an answer that does that but it seems like the suggestion from @BioGeek of using a Jupyter Notebook or the interactive shell are what you're looking for? I'd advocate just using the Jupyter Notebook if you want others to be able to interact with your code. – brobertsp Jun 29 '21 at 20:00

2 Answers2

2

You could run your script in interactive mode so that when it would normally exit, you instead drop into an interactive Python interpreter:

python -i my_script.py

You could also enter interactive mode from the script file without the command line flag using the Python code module like in this example:

import code

if __name__ == "__main__":

    # Do something.

    code.interact(local = locals())

Alternatively, you could use something like the exec keyword in a loop to execute arbitrary commands and kind of "fake it" with something like this:

if __name__ == "__main__":

    # Do something.

    try:

        while True:

            exec(input(">>> "))

    except KeyboardInterrupt:

        sys.exit()

Though this approach is a lot less clean than using interactive mode, especially for executing multi-line code.

Erick Shepherd
  • 1,403
  • 10
  • 19
2

If I understand you correctly, you want to load files into variables and play around with those variables.

I think the better option, instead of your proposed makeshift workspace, is to use a Jupyter notebook.

This video gives a good introduction on how the workflow looks like to read an Excel file and play around with it.

BioGeek
  • 21,897
  • 23
  • 83
  • 145