473,402 Members | 2,072 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,402 software developers and data experts.

problems loading modules

Hi,

I have the following weird behavior when I load modules:

>>import random
print random.randrange(10)
8
>>>
Everything is fine.
>>import random
from numpy import *

print random.randrange(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.randrange(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

Feb 5 '07 #1
4 3624
Frank wrote:
Hi,

I have the following weird behavior when I load modules:

>>>import random
print random.randrange(10)
8

Everything is fine.
>>>import random
from numpy import *

print random.randrange(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

Feb 5 '07 #2
"Frank" <su******@gmail.comwrites:
>import random
print random.randrange(10)
8
>>

Everything is fine.
>import random
from numpy import *

print random.randrange(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.random
<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

Feb 5 '07 #3
>>>>Frank <su******@gmail.comwrites:
>>>import random
from numpy import *

print random.randrange(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.random
<module 'numpy.random' from '/usr/lib/python2.4/site-packages/numpy/random/__init__.pyc'>
=======

Ganesan

--
Ganesan Rajagopal

Feb 5 '07 #4

Thanks guys!

Feb 6 '07 #5

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

Similar topics

0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
3
by: Ryan Riehle | last post by:
Hi All! Trying to upgrade to Apache 2.0.49 and getting compile errors related to mod_auth_pgsql, any clue?: make: Entering directory `/usr/src/httpd-2.0.49'...
1
by: Loading name... | last post by:
Hey I've got DNN Starter Kit (version 4.3.4) installed in my visual studio 2005. And are developing a DNN portal. Now I get this error where a module is suppose to be loaded: "Error: Adm is...
0
by: axel.grude | last post by:
Dear all, after logging on to the VSS database I got some problems when loading up Visual Studio. (it would sometimes time out loading System.web; hogging around200 Megs of Ram). I restored my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.