问题
I'm new to Python. I wrote a small script to generate ten random float values, but it keeps failing. Here's the script:
import random
for i in range(10):
x = random.random()
print x
and here's the error message:
TypeError: 'module' object is not callable'.
I can't see what the problem is. I'm pretty sure random exists!
My version is Python 2.7.6.
回答1:
If you save the following code into random.py:
import random
print(__name__)
print(random)
print(random.random)
and run it then it prints something like:
random
<module 'random' from '/.../python/import/name-shadowing/random.py'>
<module 'random' from '/.../python/import/name-shadowing/random.py'>
__main__
<module 'random' from '/.../python/import/name-shadowing/random.py'>
<module 'random' from '/.../python/import/name-shadowing/random.py'>
i.e., both random and random.random refers to the same module -- your local random.py that shadows random module from the standard library.
Perhaps it works as follows:
python -mrandomfindsrandom.pyin the current directory, imports it asrandomand and starts to run it as__main__.- It sees
import randomand imports the cached module. print(random)prints the representation of the module object to stdout.print(random.random)looks uprandomname inrandommodule. It finds it (the name refers to the module itself. And it prints it.
The solution is to avoid shadowing the names. Avoid shadowing the standard names for readability. And avoid shadowing 3rd-party names if you might use them in your code.
If the current directory is not in sys.path then it imports stdlib module:
import sys
sys.path.pop(0) # assume the script directory is the first in the Python path
import random
print(__name__)
print(random)
print(random.random)
Output
__main__
<module 'random' from '/usr/lib/python3.4/random.py'>
<built-in method random of Random object at 0x1aaf7f8>
Note: it is only for illustration. Avoid modifying sys.path in your scripts.
来源:https://stackoverflow.com/questions/27702506/why-is-calling-import-random-random-random-throwing-a-typeerror