problems loading modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank

    #1

    problems loading modules

    Hi,

    I have the following weird behavior when I load modules:

    >>import random
    >>print random.randrang e(10)
    8
    >>>
    Everything is fine.
    >>import random
    >>from numpy import *
    >>>
    >>print random.randrang e(10)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'module' object has no attribute 'randrange'
    >>>
    Here it does not work.

    >>from numpy import *
    >>import random
    >>>
    >>print random.randrang e(10)
    8
    >>>
    Now everything is back to normal.

    That means the order the modules are loaded matters! I would expect
    there is a problem with my installation because I guess this should
    normally be independent of the loaded modules.

    Here are my questions:
    1. Does anyone has this behavior too?

    2. How can I fix this problem?

    I use linux with fedora core 6 and python 2.4.4

    I appreciate any hint. Thanks!

    Frank

  • Robert Kern

    #2
    Re: problems loading modules

    Frank wrote:
    Hi,
    >
    I have the following weird behavior when I load modules:
    >
    >
    >>>import random
    >>>print random.randrang e(10)
    8
    >
    Everything is fine.
    >
    >>>import random
    >>>from numpy import *
    >>>>
    >>>print random.randrang e(10)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'module' object has no attribute 'randrange'
    >
    Here it does not work.
    numpy has a subpackage called random. You imported it when you did "from numpy
    import *".

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • Ben Finney

      #3
      Re: problems loading modules

      "Frank" <supervau@gmail .comwrites:
      >import random
      >print random.randrang e(10)
      8
      >>
      >
      Everything is fine.
      >
      >import random
      >from numpy import *
      >>
      >print random.randrang e(10)
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      AttributeError: 'module' object has no attribute 'randrange'
      >>
      >
      Here it does not work.
      "Don't do that, then."

      More specifically, 'from foo import *' is deprecated for exactly the
      reason you found here: you risk clobbering an existing name in the
      current namespace, and there's no way to determine that by looking at
      the code.

      Instead, import modules preserving a module namespace, which is the
      behaviour you get from 'import foo'. That way, all names remain
      explicit and you can see where you might be re-binding an existing
      name.
      >>import random
      >>random
      <module 'random' from '/usr/lib/python2.4/random.pyc'>
      >>import numpy
      >>random
      <module 'random' from '/usr/lib/python2.4/random.pyc'>
      >>numpy.rando m
      <module 'numpy.random' from '/usr/lib/python2.4/site-packages/numpy/random/__init__.pyc'>

      Alternatively, if you want *specific* attributes from a module or
      package to be in the current namespace, import them explicitly by
      name; the same applied above, that you can see which names in
      particular are being re-bound.
      >>import random
      >>random
      <module 'random' from '/usr/lib/python2.4/random.pyc'>
      >>from numpy import random
      >>random
      <module 'numpy.random' from '/usr/lib/python2.4/site-packages/numpy/random/__init__.pyc'>

      Again: don't use 'from foo import *', without knowing exactly why
      you're doing it. 'import foo' or 'from foo import bar' are always
      available, and usually better.

      --
      \ "I always wanted to be somebody. I see now that I should have |
      `\ been more specific." -- Lily Tomlin |
      _o__) |
      Ben Finney

      Comment

      • Ganesan Rajagopal

        #4
        Re: problems loading modules

        >>>>Frank <supervau@gmail .comwrites:
        >>>import random
        >>>from numpy import *
        >>>>
        >>>print random.randrang e(10)
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        AttributeError: 'module' object has no attribute 'randrange'
        >>>>
        Here it does not work.
        Here's a clue.

        ======
        >>import numpy
        >>numpy.rando m
        <module 'numpy.random' from '/usr/lib/python2.4/site-packages/numpy/random/__init__.pyc'>
        =======

        Ganesan

        --
        Ganesan Rajagopal

        Comment

        • Frank

          #5
          Re: problems loading modules


          Thanks guys!

          Comment

          Working...