Reputation: 23
from random import randint
This is the code I've used to import the Random module. When I run the code, it instead imports a file of mine for testing code called random.py. This was leading to all sorts of errors, so I 'permanently' deleted it in the Recycle Bin and all it's shortcuts I could find.Yet it still is there somewhere and it causes errors.My entire program depends on
x = randint(1, 100)
if x % 2 == 0:
b = 'c'
The game I'm making depends in randint to randomly create ships and submarines. I am running Windows 7.
Thanks
Upvotes: 1
Views: 118
Reputation: 1121634
Python creates a bytecode cache for each python module you import; for Python 2 the same name as the original file with the extension .pyc
is used. It is that file that is imported when you import random
, even if the source .py
file was deleted.
Use:
import random
print(random.__file__)
to locate the stale random.pyc
bytecode file and delete it.
In Python 3 these files are normally stored in a subdirectory called __pycache__
but these are ignored if there is no corresponding .py
source file. People that want to distribute just the bytecode files (to obfuscate their code in commercial software distributions, for eample) they'll have to use the compilall
script with the -b
switch, something you almost certainly did not do.
Upvotes: 3
Reputation: 61508
There will also be a random.pyc
file, either in the same folder (for Python 2.x) or a subdirectory called __pycache__
(for 3.x).
Upvotes: 1