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.
Related
Normally, if I want to run a python file beginning-to-end in pycharm, but stop at the end so I can see all the variables etc., I add a line at the end of the file like
justadummyvar = 3
And attach a breakpoint to it.
Is there a way to just tell pycharm to debug run the file and stop at the end before flushing memory so I can get the same effect without changing the file contents?
The thing I was looking for is to right click within the editor and select Run File in Python Console. From there, you can see variables stored in memory and issue new commands after the entire script runs.
You can re-run or run with debug with the buttons on the left of the little console window.
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?
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.
I'm learning how to create an automatic script in python (3.7) and I've noticed of these different behaviours:
(The python script is not relevant in this question but it works quite well so that I'm sure the answer of my question isn't here).
Case 1:
I create a batch file "test.bat" with this content:
test.bat
START "test" "C:\Users\cooper\AppData\Local\Programs\Python\Python37-32\test\v4.py"
I execute it (double click) and it opens a first window and right after closes it, and just after opens a second window with the title: "C:\Windows\py.exe" and the python script works well.
Case 2:
I create a cronjob on the cron task scheduler with an action to execute the batch file.
The cronjob is the standart one:
Trigger: on a specific time, execute it X times in a day
Action: execute the batch file
I also changed an option called: configuration (W10 in my case).
When the file is executed it opens the first window and immediately after closes it, and just after it opens the second window and immediately after close it.
I would like to run the python script but doing it through the cronjob but whatever I do, it doesn't work. I've tried other variations on the batch file using cd and directly using:
"C:\Users\cooper\AppData\Local\Programs\Python\Python37-32\python.exe" "C:\Users\cooper\AppData\Local\Programs\Python\Python37-32\test\v4.py"
but none of them works. Idon't remember well the first, but the second shows command line on System32 directory and doing nothing.
The /K option did the trick
"C:\Users\cooper\AppData\Local\Programs\Python\Python37-32\python.exe" "C:\Users\cooper\AppData\Local\Programs\Python\Python37-32\metart\v4.py" /K
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).