473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

import random module

Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55)
print x 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.

Mar 22 '06 #1
14 9352
DataSmash wrote:
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55)
print x 14


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
Mar 22 '06 #2
DataSmash wrote:
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55)
print x 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.


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/
Mar 22 '06 #3
DataSmash wrote:
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55)
print x

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
Mar 22 '06 #4
"DataSmash" <rd*@new.rr.com> writes:
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55)
print x 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?


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

--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Mar 22 '06 #5

DataSmash wrote:
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55)
print x 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.


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

Gerard

Mar 22 '06 #6
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

Mar 22 '06 #7

"Benjamin Niemann" <pi**@odahoda.de> wrote in message news:dv**********@online.de...
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. ;)
Mar 22 '06 #8
Much Thanks!
I deleted the random.pyc and renamed the script and everything is good!
R.D.

Mar 22 '06 #9
Much Thanks!
I deleted the random.pyc and renamed the script and everything is good!
R.D.

Mar 22 '06 #10
You must rename Your script.
Your script doesn't same name as calling module.

Tomas Brabenec
http://brabenec.net
DataSmash napsal(a):
Hi,
When I import the random module at the python interpreter, it works
fine:
import random
x = random.randint(1,55)
print x

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.

Mar 22 '06 #11
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

Mar 22 '06 #12
"DataSmash" <rd*@new.rr.com> writes:
* 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.

--
\ "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.written, May 2000 |
Ben Finney

Mar 22 '06 #13
Ben Finney wrote:
"DataSmash" <rd*@new.rr.com> writes:
* 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/>


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

Mar 23 '06 #14
In article <11**********************@t31g2000cwb.googlegroups .com>,
"Carl Banks" <in**********@aerojockey.com> wrote:
Ben Finney wrote:
"DataSmash" <rd*@new.rr.com> writes:
* 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/>


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
Mar 23 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Randall Smith | last post by:
How do I import a module when given the module name as input? Between __import__, imp, and rexec, I'm very lost. What I want to do seems so simple, but I don't know the best way to do it. How...
2
by: Marc Ederis | last post by:
Hello, I'm trying to create an executable with py2exe, and it uses the odbc module. The script runs fine until I use py2exe on it and run the ..exe. Then I get: -- Traceback (most recent...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
6
by: Sean Berry | last post by:
I don't know that this partilarily bad programming, but I was interested in doing the following. def functionname( module, var1, var2 ): import module I would like to be able to pass the...
3
by: Rob B | last post by:
Hello, I am just starting to learn Python and was writing a simple script on my machine (Mac OS X 10.3.4), but I can't seem to import the random module: #!/usr/bin/env python import random
14
by: Jay O'Connor | last post by:
Is there a good way to import python files without executing their content? I'm trying some relfection based stuff and I want to be able to import a module dynamically to check it's contents...
3
by: Phd | last post by:
Hi, I'm using python 2.2, I want to import a module by referring to its relative location. The reason for this is that there is another module with the same name that's already in pythonpath(...
10
by: Jia Lu | last post by:
Hi all: I try to do things below: import i Traceback (most recent call last): File "<pyshell#67>", line 2, in <module> import i ImportError: No module named i But it seems that import...
3
by: Andrew F | last post by:
I'm a linux user and I just upgraded from 2.1 to 2.5 and changed the location of a number of libraries and tools. So far I've tracked most errors, but this one has me scratching my head : $...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.