Seite 1 von 1

numpy/random NameError with python 2.7.3 numpy 1.6.1

Verfasst: Montag 14. Mai 2012, 16:21
von dietmar13
hello,

Scientific linux 6.1 (=RHEL 6.1); python 2.7.3; numpy 1.6.1
i get following error if 'random' is imported by a python script:

Code: Alles auswählen

Traceback (most recent call last):
  File "/home/ws/SW_install/RSeQC/usr/local/bin/bam_stat.py", line 24, in <module>
    from qcmodule import SAM
  File "/home/ws/SW_install/RSeQC/usr/local/lib/python2.7/site-packages/qcmodule/SAM.py", line 14, in <module>
    import random
  File "/usr/local/lib/python2.7/site-packages/numpy/random/__init__.py", line 90, in <module>
    ranf = random = sample = random_sample
NameError: name 'random_sample' is not defined
/random
"__init__.py"

Code: Alles auswählen

# To get sub-modules
from info import __doc__, __all__
from mtrand import *

# Some aliases:
ranf = random = sample = random_sample
__all__.extend(['ranf','random','sample'])

def __RandomState_ctor():
    """Return a RandomState instance.

    This function exists solely to assist (un)pickling.
    """
    return RandomState()

from numpy.testing import Tester
test = Tester().test
bench = Tester().bench
is this a bug in numpy/random or what can i do to fix this...

Re: numpy/random NameError with python 2.7.3 and numpy 1.6.1

Verfasst: Montag 14. Mai 2012, 16:43
von deets
Please use python-code-tags to set code here proper.

And is it possible that you have a local module named mtrand that shadows the numpy one? Try e.g.

Code: Alles auswählen

>>> import mtrand # if this import works, you are shadowing the numpy one.

Re: numpy/random NameError with python 2.7.3 and numpy 1.6.1

Verfasst: Montag 14. Mai 2012, 17:12
von dietmar13
thank you deets for your suggestions, but:

ImportError: No module named mtrand

Re: numpy/random NameError with python 2.7.3 and numpy 1.6.1

Verfasst: Montag 14. Mai 2012, 17:24
von BlackJack
@dietmar13: What do you get if you import `numpy.random` and then print `numpy.random.mtrand`? What attributes are defined on that module? Here it looks like this:

Code: Alles auswählen

>>> import numpy.random
>>> print numpy.random.mtrand
<module 'numpy.random.mtrand' from '/usr/lib/python2.6/dist-packages/numpy/rando
m/mtrand.so'>
>>> dir(numpy.random.mtrand)
['RandomState', '__builtins__', '__doc__', '__file__', '__name__', '__package__'
, '_rand', 'beta', 'binomial', 'bytes', 'chisquare', 'dirichlet', 'exponential',
'f', 'gamma', 'geometric', 'get_state', 'gumbel', 'hypergeometric', 'laplace', '
logistic', 'lognormal', 'logseries', 'multinomial', 'multivariate_normal', 'nega
tive_binomial', 'noncentral_chisquare', 'noncentral_f', 'normal', 'np', 'pareto'
, 'permutation', 'poisson', 'power', 'rand', 'randint', 'randn','random_integers
', 'random_sample', 'rayleigh', 'seed', 'set_state', 'shuffle', 'standard_cauchy
', 'standard_exponential', 'standard_gamma', 'standard_normal', 'standard_t', 't
riangular', 'uniform', 'vonmises', 'wald', 'weibull', 'zipf']

Re: numpy/random NameError with python 2.7.3 and numpy 1.6.1

Verfasst: Montag 14. Mai 2012, 17:51
von dietmar13
@BlackJack

Code: Alles auswählen

Python 2.7.3 (default, May 14 2012, 14:13:50) 
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy.random
>>> print numpy.random.mtrand
<module 'numpy.random.mtrand' from '/usr/local/lib/python2.7/site-packages/numpy/random/mtrand.so'>
>>> dir(numpy.random.mtrand)
['RandomState', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__', '_rand', 'beta', 'binomial', 'bytes', 'chisquare', 'dirichlet', 'exponential', 'f', 'gamma', 'geometric', 'get_state', 'gumbel', 'hypergeometric', 'laplace', 'logistic', 'lognormal', 'logseries', 'multinomial', 'multivariate_normal', 'negative_binomial', 'noncentral_chisquare', 'noncentral_f', 'normal', 'np', 'pareto', 'permutation', 'poisson', 'power', 'rand', 'randint', 'randn', 'random_integers', 'random_sample', 'rayleigh', 'seed', 'set_state', 'shuffle', 'standard_cauchy', 'standard_exponential', 'standard_gamma', 'standard_normal', 'standard_t', 'triangular', 'uniform', 'vonmises', 'wald', 'weibull', 'zipf']
I have also Python 2.6.6 installed as default python (because i need it for another program) and 2.6.6 is the python which comes with scientific linux 6.1 (=RHEL 6.1), but I start the script with <python2.7 script.py> and numpy is installed with/for python 2.7.
Could this be a problem?

Could I change the script to <import numpy.random> to bypass the error? I will try.

If I use <import numpy.random> in this script, I get errors downstream from other moduls:

Code: Alles auswählen

Traceback (most recent call last):
  File "/home/ws/SW_install/RSeQC/usr/local/bin/bam_stat.py", line 24, in <module>
    from qcmodule import SAM
  File "/home/ws/SW_install/RSeQC/usr/local/lib/python2.7/site-packages/qcmodule/SAM.py", line 23, in <module>
    import pysam
  File "/home/ws/SW_install/RSeQC/usr/local/lib/python2.7/site-packages/pysam/__init__.py", line 1, in <module>
    from csamtools import *
  File "csamtools.pyx", line 4, in init csamtools (lib/pysam/csamtools.c:32735)
  File "/usr/local/lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
ImportError: cannot import name Random

Re: numpy/random NameError with python 2.7.3 and numpy 1.6.1

Verfasst: Montag 14. Mai 2012, 18:08
von BlackJack
@dietmar13: What puzzles me is that according to your traceback, a plain ``import random`` in `/home/ws/SW_install/RSeQC/usr/local/lib/python2.7/site-packages/qcmodule/SAM.py` tries to import `numpy.random` at `/usr/local/lib/python2.7/site-packages/numpy/`, which means *that* path is in `sys.path`. Sounds broken. `sys.path` should not contain paths *into* package hierarchies — just to the root of them. Otherwise things like this might happen — submodules or packages exposed at a level where nobody expects them. That's a desaster waiting to happen.

Re: numpy/random NameError with python 2.7.3 and numpy 1.6.1

Verfasst: Montag 14. Mai 2012, 18:17
von dietmar13
perfect:

instead of:

Code: Alles auswählen

export PYTHONPATH=/usr/local/lib/python2.7/site-packages/numpy:/home/ws/SW_install/RSeQC/usr/local/lib/python2.7/site-packages:$PYTHONPATH
I have to use:

Code: Alles auswählen

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:/home/ws/SW_install/RSeQC/usr/local/lib/python2.7/site-packages:$PYTHONPATH
in .bashrc

thank you very much, BlackJack

## thread closed ##