Problems with import random and import numpy

All programs with import numpy or import random quit immediately when run in console. They work fine when run in IDLE. Example program is:

import random
x=random.random()
print(x)
input('Press Enter to quit')

The line with import random in your code snippet is fine. What error or stacktrace are you seeing?

No error. Console just quits. The program runs fine in IDLE

What do you mean by “console just quits”? Also, how are you trying to run your Python scripts?

1 Like

The following program does work in console, i.e. by double-clicking or by using cmd window

import math
x=math.pi
print(x)
input('Press Enter to quit')

I am using Windows 11. All programs run fine in IDLE.

What name did you give those scripts? The only possible reasons I can think of why the script with import random would not run – if executed on the command line, the same way as the one with import math – are (1) if you gave that script the name “random.py” (which can cause serious trouble when trying to import or use the actual random module) or (2) if you have a non-default Python install.

How are you running this program? If you are double-clicking on the script file and you see a brief flash of a console, it might be that you have an error but the console quits before you can read it. It’s better to open a fresh console and run python.exe script_name.py to see what happens.

1 Like

Thank you, Hans. The script was named random.py, so I will try renaming it. I need to re-install python first! I will get back to you. Thank you for your help.

Thank you, James. I will try your suggestion.

Hi Hans and James. Thank you very much for your suggestions which have helped me fix the problem. I have moved the folder containing my Python programs into the Python folder. Everything seems to work fine now!

1 Like