First of all, I started using Python with Eclipse and I didn't like it much. I looked into VS Code since so many people said it was great for Python development. The thing is like it a lot except for the fact that Eclipse showed in the terminal whatever the program I wrote said. However, VS Code also shows lots of unnecessary file paths.
Given a simple instruction such as a print I get vastly different outputs in the terminal depending if I'm using Eclipse or VS Code.
print(2+2)
This is what I get with VS Code, as you can see, the actual result I wanna see is after all those unnecessary file paths.
PS C:\Users\manue\Desktop\Uni\FP\Code> c:; cd 'c:\Users\manue\Desktop\Uni\FP\Code'; & 'C:\Users\manue\miniconda3\python.exe' 'c:\Users\manue\.vscode\extensions\ms-python.python-2021.10.1336267007\pythonFiles\lib\python\debugpy\launcher' '60771' '--' 'c:\Users\manue\Desktop\Uni\FP\Code\15_Oct\prueba.py' 4
Whereas in Eclipse I get just the 4 as I'd expect it to be. I tried modifying the launch.json file but somehow it doesn't work. I also tried with an extension called Code Runner which is super useful (only shows the actual output) but it doesn't let me do input statements. If I change the settings and let Code Runner run in the terminal it allows me to use input statements but there is a catch.
PS C:\Users\manue\Desktop\Uni\FP\Code\15_Oct> cd "c:\Users\manue\Desktop\Uni\FP\Code\15_Oct"
PS C:\Users\manue\Desktop\Uni\FP\Code\15_Oct> python -u "c:\Users\manue\Desktop\Uni\FP\Code\15_Oct\prueba.py"
4
The first output shows some file paths (I'm ok with it since it isn't overwhelming) but as I run my program more times, the gap between each line gets bigger. See this picture
Any suggestions would be gladly appreciated. Thanks
You can modify "console" in the launch.json to "internalConsole":
"console": "internalConsole",
Then you can get the output at the DEBUG CONSOLE panel:
But, it's suggested to get used to the debug Infos in the terminal, they can let you know what exactly going on in the VSCode.
Related
I just started to code on Python, so I downloaded the "Python 3.10 (64-bit)". The problem is that it runs the code without my permission. For example, if I am trying to type in print("example") it just runs on it's own. This means I can't do an actual coding project, it just runs the 1 piece of code right after I hit the Enter button. I don't know if I'm just blind or it's obvious, but I can't figure it out. What do I do?
Hitting enter after you type is "giving permission" to run the code that is typed (or copied)...
You can still write "scripts" this way - you define variables, functions, classes, etc, and they remain in scope of the interactive session.
If you want to "code a project," though, you should use an external IDE such as Visual Studio Code, PyCharm, Spyder, etc. The built-in IDLE is very basic, but still does allow for file execution.
Where do you write your code?
If you are using the command line, you cannot do anything to change this; you have to use a file with the extension ".py", for example "first_file.py", and write the code inside that file. When you are done or when you want to test your code, you have to open the terminal in the file folder and type python [filename].py and after that, your code will be compiled and executed in the terminal. I recommend using a code-writing program like VS Code, Atom or something like that.
I'm a noob in computer programming, today i installed vscode to learn python, everything was right, i run short and simple lines of code installed some extensions, but i dont know what happened, I'm not sure if i changed some configuration but when i try to run little lines of code (with the button) instead of running it and showing it to me in the "terminal section", it sends me to the "output" section and it just says that the code already run and its done.
Do somebody knows how to solve it please, it really makes me sad that i just ruined this program because its my first day and this already screw it.
As you can see in the picture, i run the code and it shows off something different in the output section, i wrote that but later changed it. It seem like it runs the file but only when i save it. :(
You must save the file to get the new output everytime.
For running code in Terminal with the button -
It looks like you are using Code Runner vscode extension. Which by default uses Output section to show output and cannot take input. You can change this behavior of that extension by pressing ctrl + , and going into vscode settings. Then just search code runner run in terminal You'll see an option to enable Run in Terminal feature turn that on.
So I am relatively new to programming and used to use Python's own "IDLE". After I run a ".py" file with IDLE, I am used to getting a python shell or a command window, I don't really know the terminological name for it, where I could play around with the objects inside the script.
For example, if I had a list A=[1,2,3] inside the program, after I run it I get a command console that says ">>" and I can say ">>A" which gives me "[1,2,3]" and I can add elements to A etc.
Now, I want to start using VS Code but I can't seem to find that particular thing. I have a terminal where I can run python code if I give the command "python" first, but It doesn't seem to effect anything inside the script.
I want to use that to see if some objects are working fine and to check their types etc. I add more lines to code after I try from there first, if that makes sense.
Sorry for really bad terminology, I really don't know the names but I can try even more if it's not clear.
Thanks a lot in advance.
Are you looking for the Integrated Terminal of VS Code?
Here are some ways to open the terminal:
Use the ⌃` keyboard shortcut with the backtick character.
Use the View > Terminal menu command.
From the Command Palette (⇧⌘P), use the View: Toggle Integrated Terminal command.
In the window that shows up, enter python and you'll get the Python shell you're looking for.
Try using the integrated terminal inside vs code and make sure that python and pip are properly configured. Type python in the command line and make sure the terminal points to the same folder where your program file is located.
I have an aesthetic problem with my VSCode terminal. On this picture you can see, that whenever I try to run my code, terminal also displays information about the directory folder. It's annoying and completely useless to me... Is there a way to modify terminal settings, so that this shit is not displayed? After running the code I only want to see the result - in this case words "hellow world".
In some guides I've seen people having it that way, however I can't do it.
System: MacOs; Python 3.X
Thanks in advance!
I have just written a bunch of lines of code on the Python prompt at the terminal. Now, I want to save all those lines of code to a .py file.
I am unable to find out how to do that. The only thing that I could find on StackOverflow was this answer but it shows only how to do it in an iPython notebook. I am not using an iPython notebook. I am running the code at the command line on the terminal.
I tried to follow that answer (because just in case) and ran the %save magic command on the terminal but it gave a SyntaxError.
So, how do save it?
Thanks!
See http://blog.e-shell.org/174 . As wu explains, the python prompt is using readline, and you can import a Python library to access this.
>>> import readline
>>> readline.write_history_file('/path/to/history.txt')
You can trying using another interpreter : bpython , I belive it has what you need,check it out.
Save the code you've entered to a file.
You seem to be affected by the misconception, that the python environment is workspace-centered (similar to what I know from Smalltalk and some LISP variants):
fire up with an initial workspace
modify by your liking
store the result
This is unfortunately not the case. While you can import existing files, the other option is to specify an existing file as initially to be loaded and keep the interpreter open by using the -i option.
It really depends on your terminal for the exact commands.
The general idea is to copy everything (if possible) or one page at a time from the terminal into a text editor and then clean the >>> prompts (and possibly other formatting problems) in the text editor.
But anyway, typing a lot of commands directly in the execution environment if really bad practice. At least you test a handful of lines and immediately save them in a file. IDLE is great at this game...