r/learnpython • u/JohnRT54 • Sep 24 '26
Defining and calling modules
Sorry if this is a dumb question, but what am I doing wrong here?
I have defined a function
#statuscheck.py
def statchk(n):
layoutx = [[sg.Text('Debug point')],
[sg.Output(key='-OUTPUT-', expand_x=True, expand_y=True)],
[sg.In(key='-IN-', expand_x=True)],
[sg.Button('Go'), sg.Button('Close')]]
winx = sg.Window('Where I am now', layoutx, auto_close=False, grab_anywhere=True, resizable=True, location=(0,0),return_keyboard_events=True, auto_size_text=True)
while True:
event, values = winx.read()
if event == sg.WIN_CLOSED:
break
if event == 'Go':
print("Here at ",n)
if event == 'Close':
break
I import it, but when I call it, it doesn't run, and I can't see any error messages. The call is:
statuscheck.statchk(1)
I know it's something simple I'm overlooking. Thanks.
2
u/woooee Sep 24 '26
Do you have more than one statuscheck.py file? Add something to statuecheck that will run when it is imported, i.e. not in the function or under if name etc. A print statement is fine. If you don't see the print when statuscheck is imported then you have another file somewhere.
2
u/JohnRT54 Sep 24 '26
It's quite possible, because I imported it to Bluestacks, and that is quite the performance. However, I renamed it to statuschock.py, which I know is unique. No difference in outcome. (Sadly. I was hoping it was that simple.)
1
u/smurpes Sep 25 '26
What’s the reason you’re using pydroid3 on a windows computer instead of a regular IDE like pycharm or vscode? You’re really limiting yourself by doing this since there’s not a big user base and it’s not a tool commonly used professionally. There’s not going to be a lot of community help for any IDE specific quirks.
From what I see pydroid3 has an interactive debugger. Put a breakpoint where you call statuscheck.statchk(1) and step through the code like that. This will show you step by step what the code is doing. You can also make statchk(1) return the function input instead to check that it is being called correctly.
1
u/JohnRT54 Sep 25 '26
I'm trying to write it in the form that I can run on my phone, and it's a lot easier to do this way. I've tried mirroring the phone to the pc, but I get the same results, and it's just easier this way. I have never been able to find the output to the pydroid debugger - occasionally I will get a window with the error message, more commonly it just returns to the code editor. The simplest code - calling a routine and printing the argument value, fails. I'm giving up.
1
u/JamzTyson Sep 25 '26
Where is sg imported?
1
u/JohnRT54 Sep 25 '26
In the main program. When I included it in the module, I got an error. I'm giving up - too much time wasted.
2
u/JamzTyson Sep 25 '26
statuscheck.pyneeds toimport PySimpleGUI as sg, otherwisesg.Textand other references tosgin that module will fail.I'm giving up - too much time wasted.
if you change your mind, the community is still here.
1
1
u/socal_nerdtastic Sep 24 '26
I assume you have import statuscheck on the top of your other file? Otherwise this looks ok. Does the program run ok if you put the function call into the statuscheck.py file? I'm not familiar with pysimplegui, but you have an infinite loop there that would block most other GUIs from running.
0
u/JohnRT54 Sep 24 '26
Yes, import statuscheck is in the main program, and I don't get an error for that. If I take the code out of the module and put it directly into the main program instead of calling statuscheck.statchk, it runs fine. That's why I'm confused.
1
u/socal_nerdtastic Sep 24 '26
Hmm are you coding in ipython or similar? Maybe you just need to reboot the kernel. Tell us how you are running the code and what IDE you are using
0
u/JohnRT54 Sep 24 '26
Pydroid3 under BlueStacks on a Windows PC.
0
u/socal_nerdtastic Sep 24 '26
ah. well unfortunately I know nothing about that. Try rebooting it :/
FYI using
import statuscheckonly works the first time you call it. Every other time you use that line it's ignored (part of python's optimization so that the same module isn't imported multiple times). Most normal coding environments restart the kernel every time you run code so this isn't an issue, but some don't, and I suspect this is one that doesn't and therefore the imports are being ignored.1
u/JohnRT54 Sep 24 '26
I exited BlueStacks and came back in, which should be a reboot, I think, and no change.
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 24 '26
I doubt importing is the problem here (you can verify it by calling the function within statuscheck.py and running it directly), but your formatting is totally broken. This is what I assume the code is supposed to look like:
def statchk(n):
layoutx = [
[sg.Text('Debug point')],
[sg.Output(key='-OUTPUT-', expand_x=True, expand_y=True)],
[sg.In(key='-IN-', expand_x=True)],
[sg.Button('Go'), sg.Button('Close')],
]
winx = sg.Window(
'Where I am now',
layoutx,
auto_close=False,
grab_anywhere=True,
resizable=True,
location=(0,0),
return_keyboard_events=True,
auto_size_text=True,
)
while True:
event, values = winx.read()
if event == sg.WIN_CLOSED:
break
if event == 'Go':
print("Here at ", n)
if event == 'Close':
breakI'm guessing you're using PySimpleGUI. I have no personal experience with it, so I'm not quite sure at a glance what you expect to happen and what's actually happening, but someone else should.
2
u/atarivcs Sep 24 '26
So many details you didn't mention.
Where are are you running this code? On your local computer, on a remote cloud desktop, or somewhere else? If it's on your local computer, what OS?
How exactly are you running this code? Are you using the "python" command in a terminal window on your local computer, are you using an IDE such as VS Code on your local computer, or something else?