How to Set up Visual Studio Code for Python Programming – CP or DSA

If you are looking for a setup like the one below, the following instructions might help you configure this on your workstation.

In case you are learning DSA, you should definitely check out our free A2Z DSA Course with videos and blogs.

Follow the instructions to set up for Python: 

  1. The first step is to download VS Code. If your system already has it, feel free to skip this step.
  2. After you finish setting up the configuration, we need to create a folder to store our files. Create a python.py file containing your code, an input.txt file, which is what you will give, and an output.txt file, where the output of your code will be printed.
  3. If you see, you will have a similar view with all three files next to each other. To create a similar view as the result,
    – go to view,
    – then to editor layout,
    – click on split left,
    This will separate the main python file to the left from the input and output files.


    To split input and output in two sections, have your cursor on input.txt file and then follow the same steps Editor Layout -> Split Down
  4. Since we have created and aligned the input and output files, we have to run these files. To make sure our input gives an output, we will have to configure it. Go to Terminal, select Configure Tasks, choose Create tasks.json file from the template, and then select others.
    Terminal -> Configure Tasks -> Create tasks.json file from template -> others


  5. After selecting others, you will see a new .vscode file and, in that, a tasks.json file, clear the current text in the json file and paste this.

    For Mac Users
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Compile and run",
                "type": "shell",
                "command": "python3",
                "args": [
                    "${relativeFile}",
                    "<",
                    "input.txt",
                    ">",
                    "output.txt"
                ],
                "presentation": {
                    "reveal": "never"
                },
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": {
                    "owner": "python",
                    "fileLocation": [
                        "relative",
                        "${workspaceFolder}"
                    ],
                    "pattern": {
                        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            }
        ]
    }
    

    For Windows Users
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Compile and run",
                "type": "shell",
                "command": "",
                "args": [
                    "python3",
                    "${relativeFile}",
                    "<",
                    "input.txt",
                    ">",
                    "output.txt"
                ],
                "presentation": {
                    "reveal": "never"
                },
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": {
                    "owner": "py",
                    "fileLocation": [
                        "relative",
                        "${workspaceRoot}"
                    ],
                    "pattern": {
                        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            }
        ]
    }
    

    If you are using some other OS, you can use ChatGPT to get the equivalent command.
  6. Post writing a program in python, press (cmd+shift+B) for Mac and (ctrl+shift+B) for Windows to run the code and get the output. In case you are facing some issue, we recommend you to use Bard or ChatGPT to get your errors resolved.