python - AttributeError: 'module' object has no attribute 'choice' -



python - AttributeError: 'module' object has no attribute 'choice' -

i using python3. first utilize random.choice in terminal, , works.

python 3.2.3 (default, feb 27 2014, 21:31:18) [gcc 4.6.3] on linux2 type "help", "copyright", "credits" or "license" more information. >>> import random >>> x = [1, 2, 3] >>> random.choice(x) 3

but when run in script, message:

attributeerror: 'module' object has no attribute 'choice'

here code

import random scipy import * numpy import linalg la import pickle import operator def new_pagerank_step(current_page, n, d, links): print(links[current_page]) if random.rand() > 1 - d: next_page = random.choice(links[current_page]) else: next_page = random.randint(0, n) homecoming next_page def pagerank_wikipedia_demo(): open("wikilinks.pickle", "rb") f: titles, links = pickle.load(f) current_page = 0 t = 1000 n = len(titles) d = 0.4 result = {} result = [] in range(t): result.append(current_page) current_page = new_pagerank_step(current_page, n, d, links) in range(n): result.count(i) result[i] = result.count(i) / t sorted_result = sorted(result.items(), key=operator.itemgetter(1)) pagerank_wikipedia_demo()

here, links[i](i integer) list. when run script, fails message mentioned above.

i have checked name of script not random. , there 1 file named random.py in /usr/lib/python3.2/random.py

why happens?

you masked module numpy.random object here:

import random scipy import *

the from scipy import * import brings whole lot of names, including random:

>>> scipy import * >>> random <module 'numpy.random' '/users/mj/development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/numpy/random/__init__.pyc'>

this replaced random module.

either don't utilize wildcard import, or import random module after importing scipy.

you import choice random module , refer directly, or utilize different name import bind to:

from random import selection scipy import * # utilize choice, not random.choice

or

import random stdlib_random scipy import * # utilize stdlib_random.choice, not random.choice

python python-3.x random

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -