Running Python in VS Code Terminal - python

Im really quite new to coding and brand new to Python so apologies if this is a dumb question.
I'm writing basic scripts in VS Code and when I run them the result in the terminal is just..... ugly. Instead of just printing the result of my code, it prints details about my version of Windows, a little copyright notice, the full file path to my code... then eventually gets round to executing my actual code.
Is there any way for me to configure the terminal so that it just shows my code and not all the other bits? I've already seem about an extension called Code Runner, but this prints to the "Output" tab and doesn't allow any user input

If you are using PowerShell, you can add "args": ["-NoLogo"], like this to Hides the copyright banner at startup :
  "terminal.integrated.defaultProfile.windows": "PowerShell",
  "terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell",
      "path": ["C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"],
      "args": ["-NoLogo"],
    }
  },
And you also can change the value of console in the launch.json file. But internalConsole does not accept the user input. And externalTerminal will prompt a cmd window out of the VSCode.

Maybe you can try Jupyter code cells:
https://code.visualstudio.com/docs/python/jupyter-support-py

Try jupyter on visual studio code interface
https://code.visualstudio.com/docs/datascience/jupyter-notebooks

You have to follow these steps:
install python extension for vs code.
python extension
2.After installing reopen vs code and you will see a play button at right-top corner.
play button
3.Click that button and run your code!

go to settings
search for terminal
find Code-runner : Run in Terminal and turn that off.

Related

my code is not showing is the terminal and there is no error

I was trying to run a code in vs code but the code didn't run and there is no error showing in the terminal. I made a new file and then tried to run a "Hello World!" but the code didn't run too. I uninstalled and re-installed vs code (three times) but that didn't work.
And when I say that there is no error showing I mean it. whenever I run the code nothing happens and vs code doesn't even give a hint or a sign that the code got executed or not.
First, you should understand that vscode is a lightweight editing
tool. If you want to run python files, first make sure you have
installed python correctly. You can enter command python --version
in the terminal to confirm it.
Then, please install python extension in the vscode extension store.
Then use shortcuts "Ctrl+Shift+P" and type "Python: Select
Interpreter" to choose the python interpreter.
Finally, creat *.py file and run it.
Read this docs for more details which can help you get started faster.

VS Code had a problem in running the code

I tried to run a python program in VS Code. But my program didn't run. The terminal opened and a weird arrow was there in the terminal. This is the screenshot of that.
This is the weird arrow and the program is not running. Any ideas why this is happening and how to fix it?
Thanks in advance.
Firstly, the arrows are included in the default python IDE means that VScode ran the command to execute your code. Give your pc a restart. Now, let us check if python is working or not or VS code is having some trouble. Type the following command in cmd to execute the code-
python "$PATH"
Rember to replace $PATH to the path of the file i.e where your file is stored. For eg. I've my python files stored in D drive in a python folder, so I'll use-
python "D:\Python\Hello.py"
If this works, python is working fine and if not, try reinstalling python and check the box which says Add python to Path or Environment variables. Then open VS code try to run the program again. But click the button only once and be patient because clicking it multiple times causes execute the same command again and cause a problem. It's my personal experience. Wait 5 minutes. Not works. Don't worry, there's a problem with the run extension you are using. I'll recommend the Code runner by Jun han. I personally use it. Type this in the extension search box-
formulahendry.code-runner
Install it and then try again.
Kill the terminal, and retry. If not work, restart the VSCode.

Python Newbie – How do I make VS Code running Python 3 only display the result of the code?

Whenever I run python code in the PyCharm IDE it simply and cleanly shows the output. But when I run python code in the Microsoft VS Code editor it shows the following:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\MY ACTUAL NAME\Desktop\Pythonista> python -u "c:\Users\MY ACTUAL NAME \Desktop\Pythonista\Exercise_3.py"
"RESULT OF CODE HERE"
PS C:\Users\MY ACTUAL NAME\Desktop\Pythonista>
The code executes underneath the Terminal label; to the left of the Problems, Output, and Debug Console headers. Why can’t it just show the result of my code without all the other details. The reason I ask is because I am trying to record myself doing the work, but I do not want my name broadcasted and I just want the output to look cleaner. Is this a thing? Thanks.
Edit: put code in a code block.
Press Crtl + J to open a terminal (or Powershell in your case), then run your code with the command python3 file.py.
Do this as opposed to clicking "Run" in the IDE, which is what most people do.
Note: when you open the terminal up initially, if it is full of jargon you don't want displaying, type clear (or the equivalent on Windows, which I think is cls).
you can use try and except so the errors won't appear in the terminal
example:
try:
# put your code here
except Exception:
pass
The right question that I needed to ask was, how do I shorten my path in VS Code and how do I get rid of the extra text that appears just before my code in the terminal after execution. After doing some research, it appears these questions have been asked many times. Below is how it is done for any other newbie like myself that stumbles upon this.
First, go to VS Code, File, Preferences, Settings, click button on the top right corner to open settings.json. Paste this,
"terminal.integrated.shellArgs.windows": ["-NoLogo"],
Next, while it is impossible to hide your path in VS code from the code output, you can certainly make your output look cleaner by moving the folder location from where the piece of code is stored. All together, when I executed my code, this is what it looked like before:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\MY NAME\Desktop\Pythonista> python -u "c:\Users\MY NAME\Desktop\Pythonista\Exercise_3.py"
Hello StackOverflow
And now, after moving my code file location closer to C:, and editing settings.json, when I execute my code, it looks like this,
PS C:\Pythonista> python -u "c:\Pythonista\Exercise_3.py"
Hello StackOverflow
Much cleaner, I hope this helps anyone else.

How do I find/excute Python Interactive Mode in Visual Studio Code?

I'm unable to find/execute the interactive mode in visual studio code - could you guys step-by-step me? Seems like a ridiculous question, I guess, but none of the waypoints I Googled seem to work;
"View > Other Windows > Interactive menu commands)" https://learn.microsoft.com/en-us/visualstudio/python/python-interactive-repl-in-visual-studio?view=vs-2019
I don't have "other windows"
If there's something I need to install, please let me know.
(Yes, I'm a beginner).
In VSCode, you can enter the Python interactive window in the following places:
Option 1
F1 -> Python: Create Python Interactive Window
This is the Python interactive window that comes with VSCode.
You can enter the interactive window without inputting instructions.
Please use "Ctrl+Enter" to execute the code:
Option 2
Ctrl + Shift + `
This is the cmd window that comes with the VSCode integration computer.
We need to enter the command "python" to enter the Python interactive window:
PS
For more information about using Python in Visual Studio Code, you can refer to the official VSCode documentation: Python in VSCode.
If you have the Python extension, you can use the Python Interactive feature (this is a IPython/Jupyter console, which can run parts of your code as 'cells', i.e. snippets of code executed in one go).
This article writes about the interactive window and many more possibilities of using Jupyter in VS Code: https://code.visualstudio.com/docs/python/jupyter-support-py
Best option
When editing Python code, select something and press Shift + Enter. This will open the interactive window and run your selection as a single cell.
I have personally been annoyed many times by accidentally pressing Shift + Enter instead of Enter on a line of code which (alone) would be a syntax error. Then, one day I decided to start using this feature consciously.
Terminal fallback
Press:
Ctrl+`
This opens a system shell (bash, or on Windows: Powershell or cmd).
Run python, or ipython if you have it (pip install ipython), and use the interactive window. In the terminal, you can also execute your script, by typing python myscript.py arguments.
If you type # %% in your vscode editor while editing a .py file,
then a interactive cell is created and it can be evaluated. The nice part of this is that # denotes comment in a .py file so you can save and run your code as a script

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

Categories