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?

3
  • 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? Commented Jun 29, 2021 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? Commented Jun 29, 2021 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. Commented Jun 29, 2021 at 20:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome man, exactly what I was looking for, the code.interact() stuff.
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.

Comments

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.