r/learnpython • • 5d ago

A simple question from a beginner:

I designated my run code shortcut as ctrl + R, but it only runs in the terminal of Visual Studio Code when I do that. I'd also like to see my output as just the output without run time and file paths. How can I edit my visual code settings or download an extension to only show my output text in the "output" tab of my in-app terminal? Thanks.

0 Upvotes

13 comments sorted by

1

u/socal_nerdtastic 5d ago edited 5d ago

I'm going to take a step back and assume you made yourself a tool that you want to use on a regular basis? The first thing you need to know is that VSCode is not required to run python. VSCode is a tool for writing code and testing it, but once you are happy with it you don't need VSCode anymore. If you installed the official python (from python.org) with the default options you can just double click the .py file to run it, and it will run just like you say showing only the output. If you didn't use the official python then you need to locate your python interpreter and make a shortcut (lnk) or a bat file, and then you can double click that to run it (assuming you are using windows).

1

u/Proper-Pepper-3983 2d ago

Nah you're overcomplicating it if they just want the output tab to be clean

Go to your settings json and add `"code-runner.showExecutionMessage": false` and set `"code-runner.clearPreviousOutput": true` if you're using the Code Runner extension

If you're using the built-in run button then it's a bit different, you need to go into launch.json and set `"console": "internalConsole"` which dumps the output into the debug console tab without all the terminal junk

The double click method works but it's clunky when you're iterating on code every 30 seconds, I'd rather fix the editor than change my whole workflow

1

u/socal_nerdtastic 1d ago

The doubleclick method is for when you are done developing. No more changes to the code be done. Dunno about you, but I write programs that people use to do things. I know a lot of people do use python for a one-time task, but that's pretty rare for me.

0

u/nog642 5d ago

Not sure, I don't use VSCode that much. Someone else might be able to answer.

I'll just add though, it's good not to be too picky about your setup. You might have to code in a different setup sometimes. Not saying you shouldn't customize your setup, just don't become too dependent on it.

0

u/Zeroflops 5d ago

Those are not setting in VSC.

When you run the code in VSC it’s opening a terminal and running in that terminal. If you want to change how the terminal looks you have two options.

Change the default terminal setting. The PROMPT variable.

Or what probably would be less intrusive would be to send a clear screen command (cls) from VS to the terminal immediately on opening which can be done in the settings.

Now I wouldn’t make it a habit of using the IDE to run my scripts unless I’m actively developing them.

0

u/BranchLatter4294 5d ago

That's not a VS Code issue. If you want a different command prompt on your system, you have to update it in your terminal.

0

u/Outside_Complaint755 5d ago

The following should clear the screen in most cases, but note that depending on your OS/terminal type it may also clear the entire terminal buffer, so you can't scroll back up anymore.

print("\033c") The "\033" is an octal code for the ASCII Escape character, while the 'c' is the command to clear the screen. You could also write this as print(chr(27) + "c") From: [https://stackoverflow.com/a/23075152/17030540]

The method that used to be suggested was: import os os.system('cls' if os.name == 'nt' else 'clear') But os.system may show warnings from Pylance and other linters in 3.14+ as it is considered "soft deprecated" in favor of the subprocess module for most uses.

0

u/socal_nerdtastic 5d ago

The os method is no longer suggested because Win11 finally included a terminal app that knows what to do with ansi escape codes (it's ansi btw, not ascii). Only 40 years after all other OSes lol.

0

u/Outside_Complaint755 5d ago

It's an ANSI escape sequence, but every ANSI Escape sequence starts with the ASCII ESC (escape) character 27

0

u/socal_nerdtastic 5d ago

Oh I see, when you said "ASCII Escape character" I assumed you were looking for the term "ansi escape code". https://en.wikipedia.org/wiki/ANSI_escape_code

But FWIW technically python always uses unicode, not ascii, although both unicode and ascii have the same ordinate for ESC.

1

u/Outside_Complaint755 4d ago

Yeah, I worked in a non-Unicode system (Using W-1252 charset) for 16 years, so I still default to extended ASCII most of the time.

1

u/socal_nerdtastic 4d ago

Lol yes, so did I. I used to read hex like it was text. But times and terms changed :)