Running Python code from an IDLE editor window - python

I've programmed a bit in Python for the last few days and everything has worked fine up until yesterday. Whenever I try to run a script now the shell only responds with the message RESTART and a path to where my script is stored. Here's an image:
I don't know how to fix this and can't find any solutions to it. When I'm writing the code directly into the shell everything works fine but this error only appears when I try to run a script from another file.
Thanks in advance

Except for the RESTART line, the behavior you see has nothing to do with IDLE. Run any program (without IDLE) that does not print anything with the '-i' option and you will see the same thing -- a '>>> ' prompt.
C:\Users\Terry>type testprog.py
5+5
C:\Users\Terry>python -i testprog.py
>>> quit()
C:\Users\Terry>
IDLE runs a editor window code the same as if it were run in a console (Command Prompt on Windows) with 'python -i'.

That's because your testprog.py doesn't actually output anything. Try changing it to print(5 + 5) or something similar, currently it only computes 5+5 and then it exits.

Related

VSCode Python terminal forced to use exit() command

I'm trying to run python files in VSCode and I'm running into an issue after the program ends.
Ex. code:
# simple code
print(":(")
I have to type exit() every time I want to run code again, since the code finishes and the console shows >>> instead of the typical text (Computer name:Folder Username$).
Any thoughts? Thanks!
Details:
Extensions active: Python (Microsoft), Jupyter, Pylance
Edit: Running files with python3 myFile.py in terminal (the real mac one and the VSCode one) works perfectly fine, but I have to escape with exit() when clicking the green run button.

Executing Python script without hanging terminal

Now this question might seem like an questions like this one and this one.
However unfortunately the above solutions don't work for me. I need a way to execute a Python 3.4.3 script, leave it running when the terminal closes, and have it not hang in terminal and executed directly.
pi#raspberrypi:/var/www/html/mysite/scripts $ nohup python3 my.script.py &
The above runs the script, but it hangs in terminal, I cannot enter any other commands until I stop it, by pressing CTRL + C.
Is there a way to achieve something like this:
pi#raspberrypi:/var/www/html/mysite/scripts $ nohup python3 my.script.py &
pi#raspberrypi:/var/www/html/mysite/scripts $
(Now I enter more commands, despite the script still running)
I hope I have provided enough information, hopefully you can help me, thanks!
Based on a quick google search, I found
start /b python3 my.script.py
for windows or
python3 my.script.py &
on lunix (bash).
If the ampersand doesn't work, you can use a terminal multiplexer like GNU screen or tmux for your purpose.

What does "== RESTART <path> ==" in the IDLE Shell mean?

I have a simple script I wrote, and when trying to run it (F5) , I get this msg:
================== RESTART: C:\Users\***\Desktop\tst.py ==================
I restarted the shell, reopened the script, but still, the same msg appears.
I use python 3.5.1 and I tried to simplify the script as much as possible, but I still get this result. Now my script is only one line with a simple print(1) command and I still get this msg.
Was there something wrong with the shell installation?
I have a simple script I wrote, and when trying to run it (F5)
That's the hotkey for IDLE to run a file. It is not ordering to do anything. It's a log statement to explicitly declare that your namespace is being cleared and the file is going to be ran fresh again.
no, I didn't tell it to restart
But you did... You pressed F5
The same thing is happening with my shell. In the older versions, this does not happen. I've also noticed that when I press Python 3.5.2 Module Docs, I have my Internet browser open up and I see my directory being displayed onscreen. It looks like:
C:\Users\mycomputername\AppData\Local\Programs\Python\Python35-32\DLLs.
Is that suppose to happen? Is that secured? I don't know.
I've also found that this prints out whenever I "imported" something. So if I use an import command, and put it right before the line of my random name, it will print out that "RESTART" thing. It's always at the beginning. Or what it reads as the beginning.
CIsForCookies, my guess is that you don't actually have a complete script; maybe you have just a function definition and you haven't included a line to run that function. (I had this problem and then remembered to call the function I defined; the problem went away.)
You may have made the same mistake as me and ran a program and then you wonder why RESTART is all that shows up. My program was working perfectly I just did not print anything or ask for any input so it ran and was done with the program and nothing showed up.

A python 3.5 script not printing results in windows command shell or powershell, only in the interpreter

I'm trying out some data science tutorials with python and can't get print to, well, print! when I run a .py in windows command shell or powershell. Print does work when I use the interpreter, but I have to type it in line by line (I'm not seeing how to run a .py as a file in the interpreter). I'm attaching snips of the file, and a snip of me running in the interpreter. I tried to attach snips of what happens when I run in command shell and powershell, but apparently I need at least 10 reputation points before I can post more than 2 links. Admittedly, those snips weren't interesting; there is no error and nothing printed. It just runs and returns to the prompt.
Also, as a test, I saved a .py file that simply does print ("Hello") and it does print correctly in windows command prompt and powershell.
Thanks in advance for any help!
Casie
PY script
Snip From Python Shell
Is that image from the IDLE console? To run the script you are editing, use menu Run > Run Module, or F5. Every python GUIs has an equivalent feature.
To run an arbitrary script from inside the commandline interpreter, say mywork.py: As long as it's in the working directory that you were in when you started the interpreter, you run it (once) by typing import mywork. (As you can see, its name must be a python identifier.)
Edit: You'd get to the bottom of this a lot quicker if you'd put print("Hello, world") in a script by itself and run it. From over here it looks like it would print just fine, proving there's nothing wrong with your python interpreter.
Your script has a bug, though: As soon as you enter the function random_kid(), you leave it again since you return a value on the first line. All those print statements are never executed. Why you think it works differently with %run I can't say, but for sure this function cannot print any output.

Correct way to launch and close interactive ipython session from notepad++

What I would like to do is launch an interactive ipython session from notepad++, and keep the window open (in interactive mode) after a script completes, but for the window to close once I exit from ipython. This seems like a fairly simple task, but I'm having trouble finding the answer.
In notepad++, I have entered the following for the Run command (F5):
cmd /k ipython -i "$(FULL_CURRENT_PATH)"
This works fine: it opens and runs the script I am editing using ipython, and keeps the ipython session open once the script is complete.
However, after entering the exit command, I have to enter exit a second time at the command prompt) to close the window (or close it with the mouse). It would be nice if I didn't have to enter exit twice. Is there a solution to get things working the way I want them to work?
I have tried removing /k flag (my understanding is this flag keeps the window open):
cmd ipython -i "$(FULL_CURRENT_PATH)"
However, the script does not seem to run at all in this case.
Well I discovered the answer just before I was about to post the question! Instead of deleting it altogether, I thought I would go ahead and post the resolution so that others can find it later. It turned out to be pretty simple!:
ipython -i "$(FULL_CURRENT_PATH)"
(Note that in order for this to work, ipython must be available as a program to be run from a command prompt.)

Categories

Resources