Randint annoys me

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BurnTard
    New Member
    • May 2007
    • 52

    Randint annoys me

    Hello, I am rather a newbie in python, and I'm trying to creawte a simple program that uses random. When I import random in the python program, it runs along smoothly. However, when I import it in a script, and then use print random.randint( 1,6), it tells me: "module object has no attribute 'randint'". What in the blazes am I supposed to make of that? My friend is sitting not ten inches away from me, using the same script, and it works like a charm.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by BurnTard
    Hello, I am rather a newbie in python, and I'm trying to creawte a simple program that uses random. When I import random in the python program, it runs along smoothly. However, when I import it in a script, and then use print random.randint( 1,6), it tells me: "module object has no attribute 'randint'". What in the blazes am I supposed to make of that? My friend is sitting not ten inches away from me, using the same script, and it works like a charm.
    I can get the same message in this scenario:[code=Python]>>> import random
    >>> print random.randint( 1,6)
    5
    >>> random.randint = 6
    >>> print random.randint( 1,6)
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: 'int' object is not callable
    >>> del random.randint
    >>> print random.randint( 1,6)
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    AttributeError: 'module' object has no attribute 'randint'
    >>> [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by BurnTard
      Hello, I am rather a newbie in python, and I'm trying to creawte a simple program that uses random. When I import random in the python program, it runs along smoothly. However, when I import it in a script, and then use print random.randint( 1,6), it tells me: "module object has no attribute 'randint'". What in the blazes am I supposed to make of that? My friend is sitting not ten inches away from me, using the same script, and it works like a charm.
      Posting the code here (USING [CODE] TAGS) would be very helpful.

      Comment

      • BurnTard
        New Member
        • May 2007
        • 52

        #4
        The scrip is not seriously difficult really. It consists of two lines:

        Code:
        import random
        print random.randint(1, 6)
        output in cmd:

        Traceback (most recent call last):
        File "C:\blah\blah\r andom.py", line 1, in <module>
        import random
        File "C:\blah\blah\r andom.py", line 2, in <module>
        print random.randint( 1,6)
        AttributeError: 'module' object has no attribute 'randint'

        If anyone has an actual solution, please help me!

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by BurnTard
          The scrip is not seriously difficult really. It consists of two lines:

          Code:
          import random
          print random.randint(1, 6)
          output in cmd:

          Traceback (most recent call last):
          File "C:\blah\blah\r andom.py", line 1, in <module>
          import random
          File "C:\blah\blah\r andom.py", line 2, in <module>
          print random.randint( 1,6)
          AttributeError: 'module' object has no attribute 'randint'

          If anyone has an actual solution, please help me!
          The "C:\blah\blah\r andom.py" part may actually be of interest. If there is another module in the sys.path by the name of "random" which is being found first (before the library module named "random"), that would happen.

          Comment

          • BurnTard
            New Member
            • May 2007
            • 52

            #6
            Originally posted by bartonc
            The "C:\blah\blah\r andom.py" part may actually be of interest. If there is another module in the sys.path by the name of "random" which is being found first (before the library module named "random"), that would happen.
            Please could you translate that into a language I can understand? I am new to the stuff after all. Did I interpet it correctly if I think that was a request for the following?

            C:\programfiler \python script\random.p y

            Sorry for being so lame.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by BurnTard
              Please could you translate that into a language I can understand? I am new to the stuff after all. Did I interpet it correctly if I think that was a request for the following?

              C:\programfiler \python script\random.p y

              Sorry for being so lame.
              Barton was telling you that Python may have found another file named 'random.py' before it found the Python library file 'random.py'. That would explain the problem you are having. Run this script to check your path:[code=Python]import sys
              for p in sys.path:
              print p[/code]This is the directory order Python searches for files to import. If __init__.py does not exist in the directory, it is skipped. I placed a file 'random.py' in one of the first directories in my sys.path. It contains a simple print statement.[code=Python]>>> import random
              This is module random.
              >>> random.randint( 1,6)
              Traceback (most recent call last):
              File "<interacti ve input>", line 1, in ?
              AttributeError: 'module' object has no attribute 'randint'
              >>> [/code]

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by BurnTard
                Please could you translate that into a language I can understand? I am new to the stuff after all. Did I interpet it correctly if I think that was a request for the following?

                C:\programfiler \python script\random.p y

                Sorry for being so lame.
                That's the problem! You have a module named random.py which is in python's search path. (too much detail: That path can be viewed and changed through sys.path). When you need something from the library, python goes out and searches sys.path in the order of the items in that list. Your module is being found instead to the library module.

                The best fix is to change the name of your "random.py" file and NEVER name your own work with names from the standard library unless you really want to replace that functionality.

                Comment

                • BurnTard
                  New Member
                  • May 2007
                  • 52

                  #9
                  Originally posted by bvdet
                  Barton was telling you that Python may have found another file named 'random.py' before it found the Python library file 'random.py'. That would explain the problem you are having. Run this script to check your path:[code=Python]import sys
                  for p in sys.path:
                  print p[/code]This is the directory order Python searches for files to import. If __init__.py does not exist in the directory, it is skipped. I placed a file 'random.py' in one of the first directories in my sys.path. It contains a simple print statement.[code=Python]>>> import random
                  This is module random.
                  >>> random.randint( 1,6)
                  Traceback (most recent call last):
                  File "<interacti ve input>", line 1, in ?
                  AttributeError: 'module' object has no attribute 'randint'
                  >>> [/code]
                  I did that thing with checking the system path, and it came up with a bunch of lines, but not one of them were/had the word random in them.

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by BurnTard
                    I did that thing with checking the system path, and it came up with a bunch of lines, but not one of them were/had the word random in them.
                    But I'll bet that one of the first ones was r"C:\\programfi ler\\python script"

                    Comment

                    • BurnTard
                      New Member
                      • May 2007
                      • 52

                      #11
                      Though apparently you were right, if I understood. The minute I changed the name from random.py to randomname.py, it worked perfectly! Thank you guys so much!

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by BurnTard
                        Though apparently you were right, if I understood. The minute I changed the name from random.py to randomname.py, it worked perfectly! Thank you guys so much!
                        That's what we're here for. Drop on in any old time.

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          Originally posted by BurnTard
                          I did that thing with checking the system path, and it came up with a bunch of lines, but not one of them were/had the word random in them.
                          The printed lines are names of directories, not files. I am glad that you solved your problem, and maybe learned something.

                          Comment

                          • Phil Norris
                            New Member
                            • Nov 2007
                            • 2

                            #14
                            Well this is all very fine - but where to look if renaming the file doesn't make a bit of difference.

                            I made a file random.py (duh) on Tiger Mac with Python 2.6.7
                            It tells me there is no randint in the module - but I have no file named random anywhere....

                            maybe in memory.

                            BRB when I restart this apple.

                            Comment

                            • Phil Norris
                              New Member
                              • Nov 2007
                              • 2

                              #15
                              Ahhh.. this is because on Mac (maybe on a win box too) Python compiles the module you import in your script into a file called 'yourmodule.pyc ' - I found my useless random.pyc file and deleted it.

                              No more 'trubble ert mill'

                              Comment

                              Working...