You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中运行random模块代码报AttributeError:'random'无randint属性求助

问题解决:AttributeError: 'random' has no attribute 'randint'

问题现象

同样的Python代码在PowerShell中可正常运行,但在VSCode执行时抛出错误:

AttributeError: 'random' has no attribute 'randint'

出错代码如下:

import random
a = random.randint(5, 10)
print(a)

问题根源

当前脚本文件被命名为random.py。Python导入模块时,会优先检索当前工作目录下的文件,此时你导入的不是Python自带的random标准库,而是你自己创建的同名脚本文件,而这个文件中并没有定义randint方法,因此触发报错。

解决步骤

  • 将你的脚本文件重命名为非标准库同名的名称,比如test_random.py
  • 如果当前目录下存在random.pyc缓存文件,一并删除,避免缓存残留导致问题

内容的提问来源于stack exchange,提问作者Illusioner_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 22:31:13