Issue
I have a script that I am trying to run in Terminal, but one part of it works perfectly in IDLE, and kills the script with no explanation when run in Terminal. Most of the posts I have found about similar problems seem to be from Terminal using Python 2 and IDLE using Python 3, but my script works in IDLE for BOTH Python 2 and 3 Most of the script runs fine, but calling this function kills it.
import random
import time
def gen_rndNum():
rndNum = random.randint(1,10)
print ("Your random number is:")
print (rndNum)
while 1:
print ('Waiting...')
if some_condition: # I know some_condition is not defined. It is a placeholder for the actual trigger
print ("Trigger Detected!")
gen_rndNum()
else:
#do some more stuff
time.sleep(.25) #Don't use 100% of CPU
When I run this code in IDLE, I get what I would expect:
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Then when I press the button
Waiting...
Trigger Detected!
Your random number is:
2
When I run this code in Terminal, I get:
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Then when I press the button
Waiting...
Trigger Detected!
And then nothing. It just stops. I am running this from a Raspberry Pi 3. I call the script with
python /home/pi/modules/useButtons.py
An interesting side note ( which I would also like input on, if possible) is that when run in IDLE, this script takes about 11% of available CPU power, but in Terminal, it takes 25% (the entirety of one core). Why would it take more power?
Solution
I found the issue! Although my script did not have a conflicting name, I had a different script (one I long forgot about) that was named "random.py". My script was trying to import that when run in the CLI, but IDLE imported the correct one. Make sure you never use the name of Built-in modlues for script names, even if you don't use that module in the current script. It WILL cause problems later!
Answered By - Alexander