Atttribute error

H

HMS Surprise

The snippet below causes an attribute error.

AttributeError: module 'urllib' has no attribute 'urlopen'

I am using python 2.2.3. According to the documentation at C:
\Python22\Doc\lib urllib has a function called urlopen.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import urllib

class login(CompactTest):
# Recorded test actions.
def runTest(self):
f = urllib.urlopen("http://www.python.org/")
f.read()
 
M

Marc 'BlackJack' Rintsch

HMS Surprise said:
The snippet below causes an attribute error.

AttributeError: module 'urllib' has no attribute 'urlopen'

I am using python 2.2.3. According to the documentation at C:
\Python22\Doc\lib urllib has a function called urlopen.

Do you have a file called `urllib.py` in the current directory? Then this
gets imported instead of the module in the standard library.

Add this directly after the ``import`` to see what's happening:

print urllib.__file__
print dir(urllib)

Ciao,
Marc 'BlackJack' Rintsch
 
H

HMS Surprise

Do you have a file called `urllib.py` in the current directory? Then this
gets imported instead of the module in the standard library.

Add this directly after the ``import`` to see what's happening:

print urllib.__file__
print dir(urllib)

Ciao,
Marc 'BlackJack' Rintsch

Thanks for posting Marc.

I do have a file named `urllib.py` in the current directory. I copied
it from 'C:\Python22\Lib' as I could not get rid of the 'no module
named urllib' error message, even though I appended 'C:\Python22\Lib
to sys.path'. This changed the error from module not found to a no
attribute msg.

The maxq program (IDE?, runtime enviroment? , shell?) apparently uses
jython so maybe sys.path is not the problem. This is the reason for my
thread 'sys.path'.

Thanks again,

jh
 
H

HMS Surprise

PS

Add this directly after the ``import`` to see what's happening:

print urllib.__file__
print dir(urllib)

C:\maxq\bin\testScripts\.\urllib.py
['__doc__', '__file__', '__name__', 'string']
 
G

Gabriel Genellina

print urllib.__file__
print dir(urllib)

C:\maxq\bin\testScripts\.\urllib.py
['__doc__', '__file__', '__name__', 'string']

Perhaps you get an import error and it got unnoticed? I think earlier
Python versions could leave a partially initialized module in sys.modules,
and Jython could suffer that. Because the first thing urllib does is to
import string, and the second, to import socket. Maybe it can't find
socket.py or its dependencies (that's not surprising, if Jython could not
find urllib.py in the first place).
So copying individual modules from the library isn't a good solution -
you'd have to copy a lot of things in order to fulfill the growing import
requirements. Best is to make Jython find its library - if Jython was
installed on C:\Jython, it should be at C:\Jython\Lib. Follow the advice
from John Machin on the other thread.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top