This may be a really basic question but I am not clear how to proceed. I have started using python in datacamp courses but now moving to my own terminal is giving me some issues. When I open VS code and open the file I want to work the terminal does not allow me to write, it just appears a path, I tried to run a simple code but it does not run, basically happen anything. Do you know if I have to do something specific before start running the code to enable the terminal?
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010]
pop = [2.519, 3.692, 5.563, 6.972]
plot1 = plt.scatter(year, pop)
plt.show()
And this is what I see on the screen, below the path highlighted in yellow the terminal does not allow to write
I Appreciate any suggestion, Thanks
The terminal is working fine, but there is currently a program running in it!
You called Python to execute your clase.py file. So, all input/output you do in the terminal will now go to/come from your Python program that you just called, not your shell.
You can either wait for the program to be finished (you will get another prompt then) or press Ctrl+C to abort your program. Since your program looks like it's outputting a plot somewhere, I'd assume it opens a new window for that - in this case, the terminal will return to the prompt once that window is closed.
If you need a second terminal to do something else while the program is running in the first, you can click the "+" button to open a second terminal (you can switch using the dropdown), or the "split" button right next to the "+" to open the second one at the side.
Related
I have the following problem, I want to change my IDE from Spyder to Eclipse and I like how the interactive console works in Spyder and want to make the one in Eclipse more like it.
When using the console in Spyder I could write if a == b: , hit enter and still go up a line and change the if a == b: part. It's not locked in.
In Eclipse, when opening the console with ctrl + alt + enter and typing some code in there, it executes every line of code instantly. So when I type if a == b: , it becomes unclickable and unchangable. I looked into the preference settings in pydev and couldn't see a way to change the behavior.
Unfortunately that's not really supported right now.
-- it really sends the contents to the backend line by line and doesn't let you edit those lines afterwards.
One option could be typing in an editor and then use F2 to send a line to the interactive console (it sends a line and then goes to the next one to make it possible to easily send many lines) or select the text that you want (and then use F2).
I'm having some trouble with pushing multi-line code chunks from a .py script to the interactive python pane in VS code. For reference, I'm moving over from using Rstudio as an IDE and would like the same sort of script-to-console interaction I'm used to from there.
For example, if I tried to run the following lines from a .py script:
def f(a):
print(a)
I'm able to run the def f(a): line, but rather than wait for the print(a) line to be run, the interactive pane tries to run the first line which results in:
IndentationError: expected an indented block (2416368674.py, line 1)
I am able to run multi-line chunks fine if I highlight the entire chunk and push it to the interactive pane.
I generally just want to be able to run multi-line chunks, functions, etc. line by line with the interactive pane knowing to wait if it should expect more code. I don't want to work with wrapping the code in a cell. Is there a way to fix this?
I am afraid you can not do that, it's unreasonable.
No matter you send the codes to the Interactive panel or the REPL in the terminal, it will automatically run the codes -- with an Enter press.
You want it without the automatically Enter press and wait for you to press it by yourself, or it can be smart enough to know when to keep waiting for the following codes.
But why not sends the code snippet directly instead of line by line and press the Enter automatically instead of manually press the Enter every time?
I've been trying to research this for a while, but can't find the answer. I'm using the keybinding shift+enter to send the line of code to the Terminal, but it only sends to the Python Interactive. It looks like VSCode should be able to figure out out where it should send the code automatically, but the only way I can figure it out is to uncheck...
Python > Data Science: Send Selection to Interactive Window
After doing this shift+enter would execute to the terminal.
I was curious if anyone has ran into this issue and fixed it without unchecking properties.
I'm a developer on this extension. By default the setting should be sending shift-enter to the terminal. So Send Selection to Interactive Window is false by default. We do have a popup that if you are executing a file with code cells in it (#%% markers) we prompt and ask if you want to send selection to the Interactive Window. If your setting was turned on it might have been that you said yes to this popup or changed the setting a previous time. Do you recall having changed the setting before?
I'm new to code and i have a doubt that i did not find anywhere else. When i try to open a file .py using the Python Shell (double click on Windows) it closes. It's not a code problem because i've created a new file, added this line:
print("test")
and the same thing happended. The most stranger thing is that if i press F5 at the editor, everything works fine. The other thing is that with another file, when i press F5 in the editor, the code works fine but adds a lot of new lines before the code (i did not add any new lines to the code). You can see this code that i'm talking about here and the module here or you can see all the code at GitHub
If you mean you're just double clicking the .py file, you see the shell blink into existence for a moment, then disappear, that's expected. A shell is created to run python to run the script, the script runs to completion, then exits, and the shell (no longer having a running program in it) closes as well.
If you want the output to stick around, either:
Launch a cmd.exe window directly, and invoke your program from there (since the cmd.exe window won't exit, the output will stick around), or
Add a call that prevents the program from exiting until you've had a chance to read the output to the end of your program, e.g. input('Press Enter key to exit...') or the like. As long as that input prompt doesn't get a response, the program is still running, and you can see the output.
The .py file closes immediately because it finishes its execution and exits upon the call of SystemExit. The whole process is too fast for you to be able to see the output on the console. If you want to execute a .py file without using the editor and be able to see the output as well, you can try using cmd and execute a line like this
python yourfile.py
In this way, the window will be left open until you manually close it. However, you have to setup properly to be able to use this python command in cmd, and it is very easy as you can search up online.
For second question, it is because you have printed \n 101 times at line 6 & 7. All function/method calls will be executed at the time you have called them. It does not mean that you can assign a function to a variable, and then only execute the function when you call the variable.
Every function will execute its code and return you something (as what has specified using return statement, can be void too). So if you assign a variable x to a print("\n") statement, it will print \n first and return void(nothing) back to your variable x.
I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).