My .py file closes immediately after running without giving output - python

When I run a .py file, it just opens and closes immediately. It doesn't give any output. I turned it into an .exe file but still the same result. I ran it from cmd, the result is the same. I checked the program that runs the code and found it to be Python.
HERE IS THE CODE.
from tkinter import *
app = Tk()
app.geometry("250x250")
app.title("Tkinter!")
mainloop()

I suggest you to kindly share the code(as there may be some codes that does not have an output. For example, if the code simply involves assigning values to a variable(say x=2+4) will not result in printing an output, unless specifically mentioned otherwise(print(x)). Also, ensure that you add the Python's installed location to PATH(Environment Variables). I am assuming that you are using Windows OS(as you mentioned cmd).

Related

Issues with opening a python file in a separate window from another python file

I need to open hello.py file in a new console window from another python file basemain.py
I need the hello.py file to pop open in a new python console. I am not just trying to read, run or execute the file. I know I can manually open the file with ctrl+o and open it in separate window but I am trying to use code instead of me using ctrl+o every single time.
I have already tried
1)
from subprocess import Popen
#Popen(['python3', 'C:\\Users\\sammy\\Desktop\\project\\hello.py'], shell=True)
did not work. I just saw a glimpse of command promt for 1 second then it went away. I did not see file popped up in a separate console
2)
open('C:/Users/sammy/Desktop/project/hello.py',"r")
also didn't work. I am guessing the file is being read but I need to see it popped up in a separate console. that didn't happen
3)
import os
webbrowser.open('hello.py')
just opened the internet browser. nothing else happened
4)
os.system("hello.py")
again, same thing as 1)
5)
import os
os.startfile('hello.py')
again, same thing as 1)
6)
import webbrowser
import os
webbrowser.open(os.getcwd() + "C:/Users/sammy/Desktop/project/hello.py")
again, just opened internet browser. nothing else
These are the solutions I found in other questions in stackoverflow and in other forums. none of them are making a separate console to be popped up so that I can see and edit the file and then run. some of them are just reading the file but I cannot see the file
I am using python 3.10 in Windows 10 system
I am fairly new in python, so if anyone can help, I greatly appreciate that. Thanks in advance

Tkinter library code not responding to Spyder breakpoints

I was trying to understand the Tkinter library code a little bit better, so I've been trying to place breakpoints in different places and understand the flow of code. To my surprise, the debugger does not stop at many points of the library code that I know are being executed.
I'm unclear on whether this is a spyder bug, or something funny in how Tkinter creates a loop for the GUI / interfaces with the OS.
This is the simple test-driving code that I am using:
from IPython import get_ipython
get_ipython().magic('reset -sf')
import tkinter as tk
x = tk.Pack()
I initially modified tkinter's init.py file at line 2144 to introduce a breakpoint here:
pack = configure = config = pack_configure
To my consternation, the debugger never stops here, which is very surprising to me given that commenting this line out causes an error in my test-drive code above.
I then added a simple print statement after line 2144 above,
sys.stdout.write('beep')
sys.stdout.flush()
Again, to my surprise, the 'beep' is only printed occasionally / seemingly stochastically. This is despite me clearing the workspace in the test driving script (the ipython %reset -sf command), as well as closing and re-opening anaconda/spyder.
Is there some kind of remedy to this, so that I can get more consistent behavior?
Thanks.

The IDLE editor in Python will not run my code when I do not save it

I have written code in a New file window in IDLE.
When I run the code there is no output.
Instead a dialog box appears showing a window accessing Python Folder 37-32.
When i closed the dialog box and the file I tried to create a new simple code below but I when I ran the code I got the same Dialog box.
What is wrong?
sum = 2+3 print(sum)
I have attached a screenshot showing the code and the dialog box that appears when the Module is run
Before you can execute your code, your first need to save the file. Thats the dialog box that popped up.
You should have seen a popup box like below. Did you? Is is unclear?
Save Before Run or Check
? Source Must Be Saved
OK to Save?
[OK] [Cancel]
One reason to require saving is that exception tracebacks refer to the file and line of lines that lead to the exception.
If you had saved, sum = 2+3 print(sum) would be a SyntaxError.
You can run single statements in Shell without saving.
The RESTART lines says that the shell re-initialized the environment for executing your code.
You should normally not save your code buried in the installation Scripts directory. Better to make a directory in your user directory, for instance, C:/Users/yourname/py/.
Yes, one should usually open a new question for unrelated questions. But without access to your machine, it is hard to know what happened with 'new'. It may be that IDLE could create a file under .../appdate/.../Scripts/, but your code cannot. If the open call did not raise and exception, it was likely created somewhere. Until you are more experienced, better to use absolute paths, such as C:/Users/yourname/py/new.txt.

Unable to modify python script in Windows

I'm currently using PyCharm and Sublime Text in Window to develop some Python script, but this morning something quite strange happens.
I am changing my code and testing it by running my code over test input. It is supposed to change the output text quite a lot. The output text at the end did not change at all. Then I try commenting out my output function, simply pass through the reading script. I thought "Now it should print nothing". But it prints the same output as yesterday, as if I never modified it today.
Any suggestion?
It turns out it's because my colleague has pushed an unexpected change up to repo last night...I always pulled before I end the day. The text output was forced by his logger:)
Mystery solved! Thanks for reading guys.
I've had exactly the same thing happen with Sublime. In my case I had moved / renamed the file after closing sublime. I reopened sublime some time later which reopened the old version of the file because I had left the file open when I last closed down sublime.
Have you tried closing the file and then opening it again from windows i.e. right-click => open with Sublime Text, to make sure you're editing the correct version?

Navigating python traceback from source file buffer

I'm using the current python-mode to edit my source file in top window and a python inferior shell in bottom window to see the outputs (using C-c C-c from the source file, the cursor stays in the top, source file window).
Is there a way to navigate the traceback errors while still staying in the source code window?
Also - is there a similar way to navigate errors just in the file that was actually sent (i.e. not those errors coming from called external files)?
At the moment I either do M-g M-g to jump to line number, or switch to the python shell window and navigate there to the error I want to have a look at.
This would be a tremendous efficiency boost!
Thank you very much!
It turns out there are the functions [next/previous]-error bound M-g M-n and M-g M-p that do this (for some reason unmentioned in the python mode descriptions...). I'll keep the question open if somebody has a good solution to the second part - cycle only through the errors in the current file.
realgud that I wrote has a command (and mode) for shells called realgud-track-mode. When you invoke that, the debugger you are using will be prompted. For example it could be pdb (but I prefer trepan3k since I wrote that too)
At any rate once all of this is set up, then the command realgud:goto-lang-backtrace-line which is bound to C-c ! ! will parse the line in the shell window and bring the file at the location mentioned up in another buffer.

Categories