Run Python in VSCode in python shell like IDLE
Asked Answered
W

5

8

I am well aware of the thread How to execute Python code from within Visual Studio Code

But none of them shows how to get the >>> python shell running with the file imported. Cuz I would like to call functions separately. I understand that I can get the python shell going by simply typling python in the terminal but the functions in the python file have to manually imported everytime.

Is there a way to run files in VSCode like in IDLE?

Waistcoat answered 25/10, 2019 at 10:51 Comment(0)
P
13

you can run the file in an interactive mode in VSC code terminal by using the parameter -i : python -i py_file.py

Pasia answered 24/11, 2019 at 10:19 Comment(2)
@xnegra80 This should probably be the accepted answer.Locomobile
ALTERNATIVE: VS Code "Tasks" add this to settings.json file: "tasks": [{"label": "Terminal: Python", "type": "shell", "command": "{../python.exe}","args": ["-i","py_file.py"] }] (in fileEnplane
H
6

Install the Python Extension, then press Ctrl+Shift+P (Command+Shift+P) and type Python: Start REPL. Click the command, and you'll get an interactive Python console.

enter image description here

Hilliary answered 10/6, 2021 at 2:36 Comment(0)
E
4

Assuming you have the official Python extension installed, you can search the command list (Ctrl+Shift+P) for "Python interactive" and you'll see several options that allow to do different variations of this:

python interactive

Edelman answered 25/10, 2019 at 11:31 Comment(1)
It opens jupyter which I dont think is what I wanted. Any ways to fix that?Waistcoat
A
1

Not sure if this was figured out or not, but I was having a similar question. How to simply use something like IDLE (the known '>>>' shell prompt) inside VSCodesee w/o chasing down plugins and their settings? I noticed that no answer was marked confirmed and although insightful, not what I was looking for.

I use Mac and while trying different things to address this, I found that by using/copying the path in the Python Launcher settings I got what I was looking for. Then, I validated the equivalent for Windows. Please see below.

Windows: If you are using Windows, from your Terminal pane/window in VSCode, enter the path to your python.exe, including the executable name. It is located within the "User > AppData" directory.

For example: C:\Users\YOUR_USER_ID\AppData\Local\Programs\Python\Python310\python.exe

Note that AppData is a hidden folder, so may have to change your view settings in Explorer to show Hidden Items.

MAC: If you are using MAC, from your Terminal pane/window in VSCode, enter the path to your interpreter

For example: /usr/local/bin/python3

Note: You can also do this directly from a CMD, PowerShell or a Terminal(Mac) window without having to launch IDLE, VSCode or any other coding/scripting app.

Anachronism answered 5/6, 2022 at 21:11 Comment(0)
G
1

To build on the answer already provided, you can automatically run in interactive mode by changing your settings:

Go under File>Preferences>Settings. Search for "arguments" in the search bar. Then under "Python › Terminal: Launch Args", click on the link "Edit in settings.json". Once settings.json is open, add "-i" with the quotation marks. It should look something like that:

{
    "workbench.colorTheme": "Default High Contrast",
    "terminal.integrated.localEchoEnabled": "off",
    "terminal.integrated.shellIntegration.enabled": false,
    "python.terminal.launchArgs": [
    "-i"
    ]
}

Now, it will pass the argument -i every time you run.

Glyn answered 22/1, 2023 at 0:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.