简体   繁体   English

Python:AttributeError:“模块”对象没有属性“ randrange”

[英]Python: AttributeError: 'module' object has no attribute 'randrange'

I know this could be caused by having a self-defined python file called random.py. 我知道这可能是由于拥有一个名为random.py的自定义python文件引起的。 I have searched for it, there is not file with such names, also there are no "pyc" file with this name. 我已经搜索过了,没有这样名称的文件,也没有带有该名称的“ pyc”文件。

I've also tried it by just typing the command in the terminal, and it seems to work! 我还通过仅在终端中键入命令来进行尝试,它似乎可以工作! But it doesnt work when I try to compile the file! 但是当我尝试编译文件时,它不起作用!

Any idea what the problem might be? 知道可能是什么问题吗?

Thanks! 谢谢!

import csv
import random
from numpy import *
from scipy.stats import norm

....

index = random.randrange(length)

...

First, you shouldn't do this on general principles: 首先,您不应该根据一般原则进行此操作:

from numpy import *

That shadows many built-ins like any and all with numpy versions which behave very differently. 这阴影许多内置插件就像anyallnumpy这些表现非常不同的版本。 But in this case, it's also causing your other problem, because there's a numpy.random which is shadowing the main random module: 但是在这种情况下,它还会引起另一个问题,因为有一个numpy.random遮盖了主要的random模块:

>>> import random
>>> random
<module 'random' from '/usr/lib/python3.4/random.py'>
>>> from numpy import *
>>> random
<module 'numpy.random' from '/usr/local/lib/python3.4/dist-packages/numpy/random/__init__.py'>

Note as an aside that if you're going to be generating many random numbers, using np.random instead of random is likely to be faster. np.random ,如果您要生成许多随机数,则使用np.random而不是random可能会更快。 ( import numpy as np is the standard character-saving alias.) import numpy as np是标准的字符保存别名。)

It is not a best practice to import everything from modules. 从模块导入所有内容不是最佳实践。 In this case, numpy seems to be interfering with random. 在这种情况下,numpy似乎在干扰随机性。 When I change 当我改变

from numpy import *

to

import numpy

the script runs. 脚本运行。 This would require you to reference anything you are using from numpy in the intervening code. 这将需要您在中间代码中引用numpy中正在使用的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 random.seed AttributeError: 模块“random”没有属性“randrange” - random.seed AttributeError: module 'random' has no attribute 'randrange' Python:AttributeError:&#39;module&#39;对象没有属性&#39;socketpair&#39; - Python: AttributeError: 'module' object has no attribute 'socketpair' Python AttributeError:“模块”对象没有属性“获取” - Python AttributeError: 'module' object has no attribute 'get' python - AttributeError:&#39;module&#39;对象没有属性&#39;lock&#39; - python - AttributeError: 'module' object has no attribute 'lock' Python错误:AttributeError:&#39;module&#39;对象没有属性 - Python error: AttributeError: 'module' object has no attribute Python AttributeError:“模块”对象没有属性“ Goslate” - Python AttributeError: 'module' object has no attribute 'Goslate' python - AttributeError:&#39;module&#39;对象没有属性 - python - AttributeError: 'module' object has no attribute Python-AttributeError:“模块”对象没有属性“ QueryFrame” - Python - AttributeError: 'module' object has no attribute 'QueryFrame' python:AttributeError,“模块”对象没有属性“某物” - python: AttributeError, 'module' object has no attribute 'something' Python AttributeError:“模块”对象没有“连接”属性 - Python AttributeError: 'module' object has no attribute 'connect'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM