导入随机模块 [英] import random module

查看:83
本文介绍了导入随机模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我在python解释器导入随机模块时,它可以工作

罚款:

Hi,
When I import the random module at the python interpreter, it works
fine:

随机导入
x = random.randint(1,55)
print x
14
import random
x = random.randint(1,55)
print x 14




但是,当我在python脚本中放入相同的代码时:

* random.py:


import随机


x = random.randint(1,55)

打印x


并按命令运行我得到:

Traceback(最近一次调用最后一次):

文件p:\ temp \ random.py,第7行,在?

随机导入

文件p:\ temp \ random.py,第8行,在?

x = random.randint(1,55)

AttributeError:''module"对象没有attribut''randint''


我每天都在命令行运行脚本所以必须有一些东西

具体

,随机错误模块,除非我完全错过了什么

这里。


任何想法?

谢谢,

RD



BUT, when I put the same code in a python script:
* random.py:

import random

x = random.randint(1,55)
print x

and run it at the command line, I get:
Traceback (most recent call last):
File p:\temp\random.py, line 7, in ?
import random
File p:\temp\random.py, line 8, in ?
x = random.randint(1,55)
AttributeError: ''module" object has no attribut ''randint''

I run scripts at the command line everyday so there must be something
specifically
wrong with the "random" module, unless I''m totally missing something
here.

Any ideas?
Thanks,
R.D.

推荐答案

DataSmash写道:
DataSmash wrote:

当我在python解释器导入随机模块时,它工作正常:
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55) )
print x 14
import random
x = random.randint(1,55)
print x 14



但是,当我在python脚本中放入相同的代码时:
* random.py:



BUT, when I put the same code in a python script:
* random.py:




^^^^^^


有你的问题:你把你的模块命名为随机,所以随机导入

将导致您自己的模块被导入。重命名,一切都会好起来。


Diez



^^^^^^

There is your problem: you named your module "random", so importing random
will cause your own module to be imported. Rename it, and everything will
be fine.

Diez


DataSmash写道:
DataSmash wrote:

当我在python解释器中导入随机模块时,它的工作原理很好:
Hi,
When I import the random module at the python interpreter, it works
fine:
随机导入
x = random.randint(1,55)
print x 14
import random
x = random.randint(1,55)
print x 14



但是当我把相同的代码放入python脚本:
* random.py:

随机导入

x = random.randint(1,55)
打印x

并在命令行运行它,我得到:
Traceback(最近一次调用最后一次):
文件p:\ temp \ random.py,第7行,在?
import random
文件p:\ temp \ random.py,第8行,在?
x = random.randint(1,55)
AttributeError:''module"对象没有attribut''randint''

我每天都在命令行运行脚本,所以必须有一些特别是
错误的随机错误。模块,除非我完全遗漏了
这里。



BUT, when I put the same code in a python script:
* random.py:

import random

x = random.randint(1,55)
print x

and run it at the command line, I get:
Traceback (most recent call last):
File p:\temp\random.py, line 7, in ?
import random
File p:\temp\random.py, line 8, in ?
x = random.randint(1,55)
AttributeError: ''module" object has no attribut ''randint''

I run scripts at the command line everyday so there must be something
specifically
wrong with the "random" module, unless I''m totally missing something
here.




这不是随机模块特有的,请尝试:

time.py:

----------------

导入时间

打印time.time()

----------------


别命名你的剧本''random.py''(或stdlib中的任何其他名称)。

''import random''将导入脚本本身(而不是随机模块来自

stdlib),这不是你想要的。


-

Benjamin Niemann

电子邮件:pink at odahoda dot de

WWW: http://pink.odahoda.de/


DataSmash写道:
DataSmash wrote:

当我在python解释器导入随机模块时,它可以工作
罚款:
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.rand int(1,55)
print x
import random
x = random.randint(1,55)
print x


14

但是,当我在python脚本中放入相同的代码时:
* random.py:

随机导入

x = random.randint(1,55)
打印x

并运行它在命令行,我得到:
Traceback(最近一次调用最后一次):
文件p:\ temp \ random.py,第7行,在?
导入随机文件p:\ temp \ random.py,第8行,在?
x = random.randint(1,55)
AttributeError:''module"对象没有attribut''randint''

我每天都在命令行运行脚本,所以必须有一些特别是
错误的随机错误。模块,除非我完全遗漏了
这里。


14

BUT, when I put the same code in a python script:
* random.py:

import random

x = random.randint(1,55)
print x

and run it at the command line, I get:
Traceback (most recent call last):
File p:\temp\random.py, line 7, in ?
import random
File p:\temp\random.py, line 8, in ?
x = random.randint(1,55)
AttributeError: ''module" object has no attribut ''randint''

I run scripts at the command line everyday so there must be something
specifically
wrong with the "random" module, unless I''m totally missing something
here.




只需将

随机打印

之后

导入random.py


来自lib的那个。


Daniel



Just put a
print random
after the
import random.py

You''ll probably see that Python is importing your main module instead of
the one from the lib.

Daniel


这篇关于导入随机模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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