Python Terminal Not running single python files but running from folders / workspaces In Visual Studio Code

33 Views Asked by At

recently I've ran into this issue where I would open a .py file that used to run fine, I would click run code and nothing would happen at all. when I open the folder that contains the file and run the code, the python terminal runs fine without any problems, so without opening a folder, I can't run the python terminal in VScode at all.

I searched a lot and all I found was to uninstall and reinstall with deleting the .vscode folder (clean reinstall).

after reinstalling yes it runs fine, but if I open a folder, close it and try to open a single file it doesn't run the python terminal at all again.

kept trying and asking, the solution was to close the workspace: File > close workspace. very simple solution, I didn't know about it cause I'm a beginner, and I wanted to leave this somewhere so no one spends time in frustration and having to reinstall VScode multiple times like I did, beginners issues haha.

To simplify: tried running a python files, the python terminal doesn't run at all. solution is to close the work space, File > close workspace.

1

There are 1 best solutions below

0
D.L On

vscode has a launch.json file in the folder which tells it how to behave when you click run or press F5.

i usually set the contents of mine to this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    
    
    "version": "0.2.0",
    "configurations": [
    


        // nodes
        {
            "type": "node",
            "request": "launch",
            "name": "node: Launch Program",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "type": "node",
            "request": "launch",
            "name": "node: Launch Program (External Terminal)",
            "program": "${file}",
            "console": "externalTerminal"
        },

        // pythons
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
    

}

The everything in the folder and subfolder will works as expected.

You can see my python config here (you probably only need the integratedTerminal one). I also have a couple of nodejs configs setup too which you can use.

The reason that i write this answer is that i always get a similar issue whenever setting up a new environment.