当您将其作为.py运行时,它不会加载random的特定模块。但是,如果我将其加载到python shell中,则不会有任何问题。
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import random
print(random.randint(0,9))
Run Code Online (Sandbox Code Playgroud)
我运行它...并且:
Traceback (most recent call last):
File "random.py", line 4, in <module>
import random
File "C:\Users\root\Pythin\random.py", line 5, in <module>
print(random.randint(0,9))
AttributeError: module 'random' has no attribute 'randint'
Run Code Online (Sandbox Code Playgroud)
并且在python shell中我没有问题:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> print(random.randint(0,9))
1
Run Code Online (Sandbox Code Playgroud)
我正在Windows PowerShell(Windows10 | 64bits)中运行它。使用命令python random.py。有人帮我吗?非常感谢你。