以.py运行时出现随机模块错误 [英] random module error when run as .py

查看:49
本文介绍了以.py运行时出现随机模块错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将其作为.py运行时,它不会加载random的特定模块.但是,如果我将其加载到python shell中,则不会有任何问题.

It does not load the specific module of random when you run it as a .py. But if I load it in the python shell, I have no problems.

# -*- coding: utf-8 -*-
#!/usr/bin/env python

import random
print(random.randint(0,9))

我运行它...并且:

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'

在python shell中我没有问题:

And in the python shell I have no problem:

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

我正在Windows PowerShell(Windows10 | 64bits)中运行它.使用命令python random.py.有人帮我吗?非常感谢.

I,m running it in Windows PowerShell (Windows10|64bits). Using the command python random.py. Someone gives me a hand? Thank you very much.

推荐答案

将代码的文件名更改为"myrandom.py".然后运行程序.

Change the file name of your code to "myrandom.py". And then run the program.

更新:请不要将您的Python源代码文件命名为标准模块.否则会导致错误.因为现在,Python而不是导入python的标准库RANDOM,而是导入了random.py并导致了错误.

Update: Please don't name your Python source code file to standard modules. As it will cause error. Because right now instead of importing the standard library RANDOM of python, the python imports this random.py and which causes the error.

例如不要给您的python源代码添加以下名称等:

e.g. Do not give your python source code following names etc.:

  1. random.py
  2. time.py
  3. csv.py
  4. 等... ... ... ...

这篇关于以.py运行时出现随机模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆