今天在运行文件 random.py文件时报错了 AttributeError: module ‘random’ has no attribute ‘randint’。
random.py 文件如下:
import random def v_code(): code = "" for i in range(5): num = random.randint(0, 9) char = chr(random.randint(65, 90)) add = random.choice([num, char]) code = code + str(add) return code print(v_code())
报错信息如下:
Traceback (most recent call last): File "E:/Gitee/python_study/Python_script/day15_module_lesson/常见的模块/random.py", line 7, in <module> import random File "E:\Gitee\python_study\Python_script\day15_module_lesson\常见的模块\random.py", line 20, in <module> print(v_code()) File "E:\Gitee\python_study\Python_script\day15_module_lesson\常见的模块\random.py", line 13, in v_code num = random.randint(0, 9) AttributeError: module 'random' has no attribute 'randint' Process finished with exit code 1
解决方法:
原来是因为命名.py文件为random,和系统的有冲突导致的。
更改了脚本的名称,就可以解决了。