Visual Studio Code: run Python file with arguments

前端 未结 7 779
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 02:00

Is there an easy way to run a Python file inside Visual Studio Code with arguments?

I know I can add a custom configuration in the launch.json file with the

7条回答
  •  囚心锁ツ
    2021-02-04 02:30

    You can add a custom task to do this. This deals with the tasks.json. You can add a default tasks.json file for you project (project folder). Follow these steps. Keyboard press Ctrl + Shift + B. It will prompt the following popup

    Click on the Configure Build Task If there is already a custom tasks.json file created in the following location .vscode/tasks.json editor will open it. If not, it will give a drop down of suggestions of already existing task runners.

    Our intention is to create a custom tasks.json file for our project, so to create one we need to select the Others option from the drop down. Check the screenshot below.

    Once you select the Others option, you could see a default tasks.json file will get created from the root directory of the project to the location .vscode/tasks.json. Below is an example of tasks.json.

    Now edit the tasks.json file to support Python.

    1. Change the Command property from "echo" to "Python"
    2. Keep showOutput as "Always"
    3. Change args (arguments) from ["Hello World"] to ["${file}"] (filename)
    4. Delete the last property problemMatcher
    5. Keep isShellCommand and version properties as unchanged
    6. Save the changes made

    You can now open your .py file and run it nicely with the shortcut Ctrl + Shift + B.

提交回复
热议问题