Stop Python from Opening Interactive Console on Script Quit? - python

I've had an issue, more of a nuisance, with python 3.6 specifically that I cant find an answer to after searching for it, and thinking I would be clever by looking for a python config file.
Whenever I run a script and that script quits, my command line pops open with an interactive console for that script, even though I've added exit(); quit() and tried just quit() to my script. I don't want to be forced to type quit() into the CLI every time I run my script. Is there a way to disable this functionality of Python?

Related

Beginner question about finding Python script in IDLE shell

I'm a beginner in Python with no prior programming experience, so bear with me here.
I installed Python, started playing with it (typing variables, playing with math operations) in the Shell window and everything is fine. I open a New Window and started writing a simple script. Something like this:
print (1+1)
I press Run Module, and I am asked to name and save the script first. I do so by calling it firstscript.py, and save it to my desktop.
Now I press Run Module, and in the shell window 2 is printed on the screen. Everything is fine. I close Python, and open it up again. Now in the shell window, I type firstscript.py and I get the red message NameError: name 'firstscript' is not defined.
I can run my program only if I open the script file on my desktop and press Run Module from there, but if I try to start it up directly in IDLE Shell by typing its name, I get the error message.
What did I do wrong? Thank you.
Good to see that you are starting with python.
Firstly, you can run the file directly using 'Run Module' only when you have the file open. Once you save the file and quit, you are out of the file editor and back to the terminal.
Simply typing in firstscripty.py will not work as it does not recognize the command.
To run the file from the terminal, use the below code:
python [locationOfFile\]firstscript.py
You can check out this detailed explanation: https://realpython.com/python-idle/#how-to-work-with-python-files
The problem here is the Shell doesnt know that your firstscript.py is sitting on the desktop
The simplest way i suggest using cmd with:
python C:\Users\{your user}\Desktop\firstscript.py

Running a simple script from desktop [duplicate]

This question already has answers here:
How do I run a Python program in the Command Prompt in Windows 7?
(24 answers)
Closed 7 years ago.
So I'm an extreme beginner to programming, just starting the Python class on Coursera. Using Python 2.7.10
Anyway, I made a simple print statement script in Notepad++
print "Hello World"
and saved as a python file on my desktop
newprog.py
However when I try to run it a cmd window appears and disappears and I'm not quite sure whats wrong.
The other question that this was linked as a duplicate to is about accessing python through the command prompt, which I don't have a problem with. From answers given it is now apparent to me that my dilemma was due to an erroneous belief that the interpreter would remain open after running whatever script I wrote.
Sounds like your program simply opened, ran and exited. So nothing was wrong, it just all happened a bit quick for you to see it.
You should run it from a command prompt or get an IDE like Pycharm, which will allow you to both write and run your script in one program.
To run from command prompt, use either Windows Key + R and type 'cmd' or click start and type 'cmd' into search box. Then you can drag your script to the command prompt window and press Enter to run it.
If you wanted to run it by double click, you'd need something to stop it from finishing until you'd read the message. To achieve this you can use the raw_input function, which waits for user input.
So your script would then look like
print "Hello World"
raw_input("Press Enter to exit")
Then you could double click and press enter when you are ready to exit.
Go to the command prompt window
python
then type in
execfile('path to newfile.py here')
Your file will now be executed
I'm running python 3.4.3. But it should be the same, I hope.
Go go "..\PythonXX\Lib\idlelib" and look for idle.pyw NOT idle.py and using the you're able to execute simple one line commands like the one you have up there.
From that you can also create a new file and do more complicated stuff.
If you create a shortcut to your desktop, you'll be able to access it easier.
Let me know if it helps, or at least correct path.
Your script is probably working and then finishing, the result is shown but not for long. I recommend opening the console and running your script from there, or you could use a simple batch file to run python scripts and then wait for a key press.
To open the console you can use the Windows key along the R key, Win-R (to run a new process) and write cmd, or you look for cmd in your Window's start menu.
With the console opened, you must locate the path where your script is, you can use the cd (Change Directory) to get there, for example:
cd C:\Users\your_name\Desktop
and then write:
python newprog.py
to run your script.
Another option is to use this simple batch file (save it as python34.bat or similar, but the extension must be .bat, put it wherever you like):
#ECHO OFF
C:\Python34\python.exe %*
pause
#ECHO ON
And then use that to run your scripts by right clicking a python script file, open with (run with) and use this batch script as default (if you want). Also, if you have another version of Python, or is installed elsewhere, you must change the "C:\Python34\" part.
This is a computer we're talking about here. It might take you triple the time it takes a computer to multiple two numbers for example. With this notion in mind, the computer quickly prints then exits.
raw_input() # at the end of script wait for user to supply input, delaying script exit

function that stop the script in python like stop() in R

I am writing a script in python and I would like to stop the scipt in the middle to see the objects and things. I used sys.exit() in pyscripter and everything is fine, but when I switch to spyder, the python interpreter is killed and I cannot read anything after the script is stopped.
How to fix it?

Is there a command or a piece of code that prevents the Python(command line) from closing immediately after executing the code?

I need your help. I'm a newbie and I'm learning python. I know to write basic codes. But, when I execute the code in Python(command line) it closes immediately. Is there any piece of code that can prevent this from happening or a trick? Please help me out. Cheers!
P.S: I use Python 2.7 in Windows.
If you're seeing a Command Prompt open and immediately close when you double-click your .py file, that's to be expected - it's not how you're supposed to run a console-based Python script.
What you should do is start a Command Prompt via the Start menu, then run your program by typing c:\python27\python.exe myscript.py or similar.
Alternatively, use a Python IDE (eg. Idle) or an editor (eg. Scite) that can run Python scripts.
The usual way to do this is from outside of Python, as RichieHindle's answer shows.
However, if you want to create a .py script that you can double-click, and it will wait for a response before exiting, you can just add this to the end of your program:
raw_input('Press ENTER to quit')
(If you've written .BAT files before, this is similar to writing PAUSE in a batch file.)
python 2
raw_input('Press Enter To Continue')
python 3
input('Press Enter To Continue')

How does one kill a python script without killing iPython?

I wrote a script in python with a wx.Frame, and it has an exit function that calls sys.exit() when the user has clicked the close button. I'd like to be able to run this script from iPython, but when the user clicks the close button, sys.exit() kills the running python script as well as iPython. What could I use in place of sys.exit() to kill only the python script, not iPython?
Thanks!
I am not familiar with iPython but with a little searching I found a page:
http://ipython.scipy.org/moin/InterupptingThreads, I infer from this that all you might have to do is "raise SystemExit".

Categories

Resources