2

I have Visual Studio Code 1.74.3 with the Microsoft Python extension v2022.20.2.

I am talking about the button to run a python file and specifically not about run/debug configurations or tasks.

enter image description here

Per default this button seems to use the configured python interpreter and runs: python.exe filename.py.

However, I want to run files as a module: python.exe -m filename

Is that possible somehow?

I found the setting "python.terminal.launchArgs" to add the "-m", but then there is still the problem that just the filename is required without the ".py" extension.

0

2 Answers 2

2

You can use Code Runner extensions and add your own configuration to customize the run command.

After installing the extension, add the following configuration in the settings.json and the running command is python -m filename.

    "code-runner.runInTerminal": true,
    "code-runner.customCommand": "python -m $fileNameWithoutExt",

Then use Ctrl+Shift+P --> Run Custom Command to execute the command. shortcut key Ctrl+Alt+K.

enter image description here

This is the execution effect:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

0

You can do that with the python debugger. There it is possible to add specific launch configurations. When creating the launch.json file, select python module.
I'm not sure if you can start it without the debugger attached.

This should include all the details you need.

2 Comments

I want to change the "run python file" button, but launch configurations don't affect the button's behaviour. I could add a launch configuration for every single file that I want to run as a module, but still I would have to select the correct configuration instead of just clicking the button.
You might be able to make a generic launch configuration with a variable (e.g. ${file}, as used in the template "Python: Current File") as module name. Then you just need to use the debug button instead of the run button. But The Code Runner extension as suggested by JialeDu might be a more convenient solution for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.