Spyder 5.1 - problem with run script button in console - python

Normally in left corner of IPython console is button to "run script/stop the current command" but in my program it stuck in "stop" form. I can run scripts with F5 or from the toolbar. While the script is running, the script button in console works, and the script can be stopped with it, but the button doesn't change into "run script" afterwards.
I have absolutely no idea what is responsible. I tried to restart Spyder, restart computer, even reinstall Spyder and reset it to factory defaults. Nothing helped.

This is by design in the IDE. Since you could have multiple files running within the same IDE session, the terminal wouldn't know which one you wanted to run at any given point. That's why the run button/shortcut are specific to one file or selection and are on the top bar. The console can only stop the execution of the currently running statement.
If you want to repeat the statement/file you just ran, type %rerun or %rerun N where N is line number of the command you want to rerun. Other options are here: https://ipython.readthedocs.io/en/stable/interactive/magics.html

Related

"Run Python File in Terminal" not working

I just pressed that triangle to run the code. I want that button to work because I want to use the debug mode from that button. When I click it, it tries to run three times and it somehow got into anaconda. I checked Python default interpreter path in preferences>settings and it is set to C:\Users\ucanc\AppData\Local\Programs\Python\Python39\python.exe, not the anaconda one. I am pretty new to this and I do not know what is going on.
edit: It seems like it is running as WSL termial that is why it has name#LAPTOP-xxxx:/mnt/c/ in the front.

CodeRunner Setup in VSC for Python

I've installed VSC and added to it the C/C++ and CodeRunner extensions, and it all worked perfectly. I've set CodeRunner to run in Terminal and I haven't done much else to the settings.
However, I installed the Python extension now, and the CodeRunner 'Run' button displays 2 options when I click it: Run Code and Run Python File in Terminal. When I was running a C program previously, one click on 'Run' would do it.
Is there any way to eliminate the extra options when running Python code? Essentially, I just want the program to execute with one click on 'Run'. Seems like the code executes regardless of the option I choose. (See the attached picture). Thank you.
Reason:
In VS Code, the green run button provided by the "Python" extension is to execute the command "Run Python File in Terminal". It only requires us to click once to run python files in the Terminal of VS Code:
When we also use the extension "Code Runner", the run button ("Run Code") provided by it defaults to output results in "OUTPUT". In the previous use, this button will cover the green button provided by the "python" extension, but now that they are integrated, we can choose the execution method.
For setting "code-runner.runInTerminal": false, it determines whether the "code runner" runs in the "Terminal", the commands they execute are different:
"Run Code":python -u "/python_file.py"
"Run Python File in Terminal": /python.exe /python_file.py
Solution: You could use F5 to debug the code or try to close the "Code Runner" extension. (Click "Enable" to restore the use of the extension.)
Reference: Run Python File in VS Code.

VS Code is now asking if I want to run the file normally or to run it in terminal

The thing is that, it never asked me this before, is there a way to fix this? Because clicking 2 times the start button to run the program is a little annoying. Thanks!
In VS Code, the green run button provided by the "Python" extension is to execute the command "Run Python File in Terminal". It only requires us to click once to run python files in the Terminal of VS Code:
If you also use other extensions, such as: "Code Runner", the run button ("Run Code") provided by it defaults to output results in "OUTPUT". In the previous use, this button will cover the green button provided by the "python" extension, but now that they are integrated, we can choose the execution method.
You could use F5 to debug the code or try to close the "Code Runner" extension. (Click "Enable" to restore the use of the extension.)

Python.exe opens in a new console window

I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome

PyCharm, stop the console from clearing every time you run the program

So I have just switched over from Spyder to PyCharm. In Spyder, each time you run the program, the console just gets added to, not cleared. This was very useful because I could look through the console to see how my changes to the code were changing the outputs of the program (obviously the console had a maximum length so stuff would get cleared eventually)
However in PyCharm each time I run the program the console is cleared. Surely there must be a way to change this, but I can't find the setting. Thanks.
In Spyder the output is there because you are running iPython.
In PyCharm you can get the same by pressing on View -> Scientific Mode.
Then every time you run you see a the new output and the history there.

Categories