import random module

D

DataSmash

Hi,
When I import the random module at the python interpreter, it works
fine:
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.
 
D

Daniel Dittmar

DataSmash said:
Hi,
When I import the random module at the python interpreter, it works
fine:
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.

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
 
D

Diez B. Roggisch

DataSmash said:
Hi,
When I import the random module at the python interpreter, it works
fine:

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

^^^^^^

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
 
J

Jorge Godoy

DataSmash said:
Hi,
When I import the random module at the python interpreter, it works
fine:

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?

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

--
Jorge Godoy <[email protected]>

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

Benjamin Niemann

DataSmash said:
Hi,
When I import the random module at the python interpreter, it works
fine:

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.

This is not specific to the random module, try:

time.py:
 
G

Gerard Flanagan

DataSmash said:
Hi,
When I import the random module at the python interpreter, it works
fine:

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.

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

Gerard
 
M

Michael Tobis

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
 
R

Richard Brodie

Benjamin Niemann said:
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.

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

DataSmash

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

DataSmash

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

Michael Tobis

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
 
B

Ben Finney

DataSmash said:
* random.py:

import random

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 DeprecationWarning; in Python 2.7,
the import behaviour specified in the PEP will be the default.
 
C

Carl Banks

Ben said:
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/>

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
 
J

Just

"Carl Banks said:
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.

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

Just
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,132
Latest member
AnneHarpur

Latest Threads

Top