Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's random module made inaccessible by Numpy's random module

When I call random.sample(arr,length) an error returns random_sample() takes at most 1 positional argument (2 given). After some Googling I found out I'm calling Numpy's random sample function when I want to call the sample function of the random module. I've tried importing numpy under a different name, which doesn't fix the problem. I need Numpy for the rest of the program, though.

Any thoughts? Thanks

like image 528
Peter Becich Avatar asked Oct 21 '11 22:10

Peter Becich


2 Answers

Sounds like you have something like

import random
from numpy import *

and random is getting clobbered by the numpy import. If you want to keep the import * then you'll need to rename random:

import random as rnd    # or whatever name you like
from numpy import *

Alternatively, and probably better, is to import numpy as a module instead of just yanking it all into your module's namespace:

import random
import numpy as np      # or leave as numpy, or whatever name you like
like image 151
Ethan Furman Avatar answered Oct 16 '22 17:10

Ethan Furman


This shouldn't happen. Check your code for bad imports like from numpy import *.

like image 24
Fred Foo Avatar answered Oct 16 '22 16:10

Fred Foo