import random module

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

    #1

    import random module

    Hi,
    When I import the random module at the python interpreter, it works
    fine:[color=blue][color=green][color=darkred]
    >>> import random
    >>> x = random.randint( 1,55)
    >>> print x[/color][/color][/color]
    14[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    BUT, when I put the same code in a python script:
    * random.py:

    import random

    x = random.randint( 1,55)
    print x

    and run it at the command line, I get:
    Traceback (most recent call last):
    File p:\temp\random. py, line 7, in ?
    import random
    File p:\temp\random. py, line 8, in ?
    x = random.randint( 1,55)
    AttributeError: 'module" object has no attribut 'randint'

    I run scripts at the command line everyday so there must be something
    specifically
    wrong with the "random" module, unless I'm totally missing something
    here.

    Any ideas?
    Thanks,
    R.D.

  • Diez B. Roggisch

    #2
    Re: import random module

    DataSmash wrote:
    [color=blue]
    > Hi,
    > When I import the random module at the python interpreter, it works
    > fine:[color=green][color=darkred]
    >>>> import random
    >>>> x = random.randint( 1,55)
    >>>> print x[/color][/color]
    > 14[color=green][color=darkred]
    >>>>[/color][/color]
    >
    > BUT, when I put the same code in a python script:
    > * random.py:[/color]

    ^^^^^^

    There is your problem: you named your module "random", so importing random
    will cause your own module to be imported. Rename it, and everything will
    be fine.

    Diez

    Comment

    • Benjamin Niemann

      #3
      Re: import random module

      DataSmash wrote:
      [color=blue]
      > Hi,
      > When I import the random module at the python interpreter, it works
      > fine:[color=green][color=darkred]
      >>>> import random
      >>>> x = random.randint( 1,55)
      >>>> print x[/color][/color]
      > 14[color=green][color=darkred]
      >>>>[/color][/color]
      >
      > BUT, when I put the same code in a python script:
      > * random.py:
      >
      > import random
      >
      > x = random.randint( 1,55)
      > print x
      >
      > and run it at the command line, I get:
      > Traceback (most recent call last):
      > File p:\temp\random. py, line 7, in ?
      > import random
      > File p:\temp\random. py, line 8, in ?
      > x = random.randint( 1,55)
      > AttributeError: 'module" object has no attribut 'randint'
      >
      > I run scripts at the command line everyday so there must be something
      > specifically
      > wrong with the "random" module, unless I'm totally missing something
      > here.[/color]

      This is not specific to the random module, try:

      time.py:
      ----------------
      import time
      print time.time()
      ----------------

      Don't name your script 'random.py' (or any other name from the stdlib).
      'import random' will import the script itself (not the random module from
      the stdlib), which is not what you want.

      --
      Benjamin Niemann
      Email: pink at odahoda dot de
      WWW: http://pink.odahoda.de/

      Comment

      • Daniel Dittmar

        #4
        Re: import random module

        DataSmash wrote:[color=blue]
        > Hi,
        > When I import the random module at the python interpreter, it works
        > fine:[color=green][color=darkred]
        >>>> import random
        >>>> x = random.randint( 1,55)
        >>>> print x[/color][/color]
        > 14
        >
        > BUT, when I put the same code in a python script:
        > * random.py:
        >
        > import random
        >
        > x = random.randint( 1,55)
        > print x
        >
        > and run it at the command line, I get:
        > Traceback (most recent call last):
        > File p:\temp\random. py, line 7, in ?
        > import random
        > File p:\temp\random. py, line 8, in ?
        > x = random.randint( 1,55)
        > AttributeError: 'module" object has no attribut 'randint'
        >
        > I run scripts at the command line everyday so there must be something
        > specifically
        > wrong with the "random" module, unless I'm totally missing something
        > here.[/color]

        Just put a
        print random
        after the
        import random.py

        You'll probably see that Python is importing your main module instead of
        the one from the lib.

        Daniel

        Comment

        • Jorge Godoy

          #5
          Re: import random module

          "DataSmash" <rdh@new.rr.com > writes:
          [color=blue]
          > Hi,
          > When I import the random module at the python interpreter, it works
          > fine:[color=green][color=darkred]
          >>>> import random
          >>>> x = random.randint( 1,55)
          >>>> print x[/color][/color]
          > 14[color=green][color=darkred]
          >>>>[/color][/color]
          >
          > BUT, when I put the same code in a python script:
          > * random.py:
          >
          > import random
          >
          > x = random.randint( 1,55)
          > print x
          >
          > and run it at the command line, I get:
          > Traceback (most recent call last):
          > File p:\temp\random. py, line 7, in ?
          > import random
          > File p:\temp\random. py, line 8, in ?
          > x = random.randint( 1,55)
          > AttributeError: 'module" object has no attribut 'randint'
          >
          > I run scripts at the command line everyday so there must be something
          > specifically
          > wrong with the "random" module, unless I'm totally missing something
          > here.
          >
          > Any ideas?[/color]

          Don't call your script "random.py" .

          --
          Jorge Godoy <godoy@ieee.org >

          "Quidquid latine dictum sit, altum sonatur."
          - Qualquer coisa dita em latim soa profundo.
          - Anything said in Latin sounds smart.

          Comment

          • Gerard Flanagan

            #6
            Re: import random module


            DataSmash wrote:
            [color=blue]
            > Hi,
            > When I import the random module at the python interpreter, it works
            > fine:[color=green][color=darkred]
            > >>> import random
            > >>> x = random.randint( 1,55)
            > >>> print x[/color][/color]
            > 14[color=green][color=darkred]
            > >>>[/color][/color]
            >
            > BUT, when I put the same code in a python script:
            > * random.py:
            >
            > import random
            >
            > x = random.randint( 1,55)
            > print x
            >
            > and run it at the command line, I get:
            > Traceback (most recent call last):
            > File p:\temp\random. py, line 7, in ?
            > import random
            > File p:\temp\random. py, line 8, in ?
            > x = random.randint( 1,55)
            > AttributeError: 'module" object has no attribut 'randint'
            >
            > I run scripts at the command line everyday so there must be something
            > specifically
            > wrong with the "random" module, unless I'm totally missing something
            > here.
            >
            > Any ideas?
            > Thanks,
            > R.D.[/color]

            your script is called 'random', I think changing this will help.

            Gerard

            Comment

            • Michael Tobis

              #7
              Re: import random module

              1) remove the file random.pyc in your working directory

              2) rename the file random.py to something else and run it.

              The import mechanism is looking for random.pyc or random.py, but you
              have already named your file that! Your current directory is above your
              library directory in python's search path, so it finds that before it
              finds the library module.

              mt

              Comment

              • Richard Brodie

                #8
                Re: import random module


                "Benjamin Niemann" <pink@odahoda.d e> wrote in message news:dvrvnf$gu3 $1@online.de...
                [color=blue]
                > Don't name your script 'random.py' (or any other name from the stdlib).
                > 'import random' will import the script itself (not the random module from
                > the stdlib), which is not what you want.[/color]

                I discovered long, long ago that giving your variables meaningful
                names only leads to confusion. ;)


                Comment

                • DataSmash

                  #9
                  Re: import random module

                  Much Thanks!
                  I deleted the random.pyc and renamed the script and everything is good!
                  R.D.

                  Comment

                  • DataSmash

                    #10
                    Re: import random module

                    Much Thanks!
                    I deleted the random.pyc and renamed the script and everything is good!
                    R.D.

                    Comment

                    • Tomas Brabenec

                      #11
                      Re: import random module

                      You must rename Your script.
                      Your script doesn't same name as calling module.

                      Tomas Brabenec



                      DataSmash napsal(a):[color=blue]
                      > Hi,
                      > When I import the random module at the python interpreter, it works
                      > fine:
                      >[color=green][color=darkred]
                      >>>> import random
                      >>>> x = random.randint( 1,55)
                      >>>> print x
                      >>>>[/color][/color]
                      > 14
                      >
                      >
                      > BUT, when I put the same code in a python script:
                      > * random.py:
                      >
                      > import random
                      >
                      > x = random.randint( 1,55)
                      > print x
                      >
                      > and run it at the command line, I get:
                      > Traceback (most recent call last):
                      > File p:\temp\random. py, line 7, in ?
                      > import random
                      > File p:\temp\random. py, line 8, in ?
                      > x = random.randint( 1,55)
                      > AttributeError: 'module" object has no attribut 'randint'
                      >
                      > I run scripts at the command line everyday so there must be something
                      > specifically
                      > wrong with the "random" module, unless I'm totally missing something
                      > here.
                      >
                      > Any ideas?
                      > Thanks,
                      > R.D.
                      >
                      >[/color]

                      Comment

                      • Michael Tobis

                        #12
                        Re: import random module

                        Wow. Six simultaneous responses! Python rocks!

                        Anyway, I actually tried it, and persisted through the secondary
                        confusion about the lurking .pyc file, so even though I'm in sixth
                        place I get points for completeness...

                        mt

                        Comment

                        • Ben Finney

                          #13
                          Re: import random module

                          "DataSmash" <rdh@new.rr.com > writes:[color=blue]
                          > * random.py:
                          >
                          > import random[/color]

                          Now that you've tripped over this ambiguity of Python's current
                          'import' behaviour, you may be interested to know that the behaviour
                          will change to solve this:

                          <URL:http://www.python.org/dev/peps/pep-0328/>

                          In brief: Python will disambiguate relative imports (from the current
                          package) from absolute imports (from the sys.path), by only allowing
                          relative imports by a specific syntax; the default import behaviour
                          will be absolute import.

                          In line with Python's procedure for introducing changes to behaviour,
                          the new behaviour will be introduced in backward-compatible stages. In
                          Python 2.5, a 'from __future__ import absolute_import ' will enable the
                          import behaviour specified in the PEP; in Python 2.6, the Python 2.4
                          behaviour will occur but raise a DeprecationWarn ing; in Python 2.7,
                          the import behaviour specified in the PEP will be the default.

                          --
                          \ "I may disagree with what you say, but I will defend to the |
                          `\ death your right to mis-attribute this quote to Voltaire." -- |
                          _o__) Avram Grumer, rec.arts.sf.wri tten, May 2000 |
                          Ben Finney

                          Comment

                          • Carl Banks

                            #14
                            Re: import random module

                            Ben Finney wrote:[color=blue]
                            > "DataSmash" <rdh@new.rr.com > writes:[color=green]
                            > > * random.py:
                            > >
                            > > import random[/color]
                            >
                            > Now that you've tripped over this ambiguity of Python's current
                            > 'import' behaviour, you may be interested to know that the behaviour
                            > will change to solve this:
                            >
                            > <URL:http://www.python.org/dev/peps/pep-0328/>[/color]

                            I don't believe this change will solve the problem at hand, at least
                            there's nothing in the PEP that says it will. "import random" is an
                            absolute import even in random.py is in the current directory, because
                            current directory is in sys.path. Unless there's a change sys.path
                            planned, the shadowing should still happen.


                            Carl Banks

                            Comment

                            • Just

                              #15
                              Re: import random module

                              In article <1143079535.649 503.127170@t31g 2000cwb.googleg roups.com>,
                              "Carl Banks" <invalidemail@a erojockey.com> wrote:
                              [color=blue]
                              > Ben Finney wrote:[color=green]
                              > > "DataSmash" <rdh@new.rr.com > writes:[color=darkred]
                              > > > * random.py:
                              > > >
                              > > > import random[/color]
                              > >
                              > > Now that you've tripped over this ambiguity of Python's current
                              > > 'import' behaviour, you may be interested to know that the behaviour
                              > > will change to solve this:
                              > >
                              > > <URL:http://www.python.org/dev/peps/pep-0328/>[/color]
                              >
                              > I don't believe this change will solve the problem at hand, at least
                              > there's nothing in the PEP that says it will. "import random" is an
                              > absolute import even in random.py is in the current directory, because
                              > current directory is in sys.path. Unless there's a change sys.path
                              > planned, the shadowing should still happen.[/color]

                              Correct. See also http://python.org/sf/946373 for more explanations and
                              also more of the same confusion.

                              Just

                              Comment

                              Working...