7

Here is the code

import random

print("Hello", end="")
print("twice")

and a screenshot of the code

enter image description here

When I execute this code it for some reason is running twice. The problem seems to be from the import random statement, because if I either remove that statement or import some other module it works fine.

What could be the reason for this, should I reinstall Python on my system.

Knight
  • 193
  • 1
  • 1
  • 6

5 Answers5

26

There's nothing wrong with python.

The reason is simple:

Your module is importing itself (because it is also named random) - this has to do with the lookup mechanics of python. python will try to import from your root folder first, before modules from pythonpath are imported.

From the docs:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • 1
    Oh my god! This was happening to me and it took me ages to figure out! Thanks! – Grezzo Mar 01 '18 at 11:55
  • Hello. I'm a python beginner and my script didn't import itself but it did run twice (compiled to exe and started from the windows explorer, or called from another application). Eventually managed to figure this is due to the syntax error i made, i used "exit(code)" instead of "sys.exit(code)" so the runtime error was occurring, but still i'm a bit puzzled, can anyone explain why this happens? – miodrag Jul 30 '23 at 19:27
5

Since your file (module) is called random.py, import random will import this very file.

Now, what does "import" mean? The statement import something will cause Python to lookup the name something, starting with the current directory. Therefore, import random will result in an import of this very file, since its name will shadow the build-in random. Besides, if the name to import is already in the namespace, then the import statement is ignored.

Once the module to import has been located, its code is executed.

As a result, the flow of your script is as follow:

  • Lookup the random.py name
  • Add random to the namespace
  • Execute the code contained in random.py
    • The random name already exists in the namespace, so the import random statement is ignored
    • Print the text
  • Print the text
Right leg
  • 16,080
  • 7
  • 48
  • 81
1

The reason for this is you've named the script random.py and inside it you import random.

random will not import the built-in random module but, rather, the random module you've created. This leads to the script executing the same statements twice (and also leads to other ugly errors if you tried and import something from random, like from random import randrange.)

Renaming the script leads to normal behavior.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
0

Because your scripts is called random.py, so when you import random you are executing your script as well. Mind to name correctly your scripts.

alec_djinn
  • 10,104
  • 8
  • 46
  • 71
0

your python script is named random.py so when you import random it import itself , in python when you import module it will run it.

therefor you get print twice. rename your script or remove the import

ddor254
  • 1,570
  • 1
  • 12
  • 28