简体   繁体   中英

VS Code How to make terminal run Python functions like IDLE?

Apologies if this has already been solved, but I was unable to find the solution. I am trying to run my python files like I could in IDLE. I remember in a previous device, I had somehow changed a setting so I was able to type in the terminal functions from the python file being run and it would run the function.

Example:

def main ():

    def function_2 ():
        print ('Value of x at start of the function: ', x)
        x = x + 10
        print ('Value of x at end of the function: ', x)

    x = 5
    print ('Value of x in main before calling function: ', x)
    function_2()
    print ('Value of x in main before calling function: ', x)

In IDLE, I would run the python file, and then run main() in the idle terminal and it would run. I was wanting to do the same, but with VS Code. Any help would be appreciated. Much thanks.

try this

def main ():

    def function_2 ():
        print ('Value of x at start of the function: ', x)
        x = x + 10
        print ('Value of x at end of the function: ', x)

    x = 5
    print ('Value of x in main before calling function: ', x)
    function_2()
    print ('Value of x in main before calling function: ', x)

main()

Add this

"python.defaultInterpreterPath": "python",
"python.terminal.launchArgs": ["-i"],

to settings.json , which you can open with Ctrl+Shift+P then >Preferences: Open Settings (JSON)

This runs the command on the open terminal so you will have to close the python terminal every time.

I haven't figured out how to do it better. So don't consider this resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM