problem with "ImportError: No module named..." and sockets

D

Daniel

Hello,

I'm trying to build a very simple IPC system. What I have done is
create Data Transfer Objects (DTO) for each item I'd like to send
across the wire. I am serializing these using cPickle. I've also
tried using pickle (instead of cPickle), but I get the same response.

Below is the code. I'll put the rest of my comments after the code

[BEGIN CODE]
#!/usr/bin/python
import SocketServer
import os, sys
newpath = os.path.normpath( os.path.join( __file__, "../../.." ))
sys.path.insert(0, newpath)

from pop.command.UpdateCommand import *
import cPickle

class RequestHandler(SocketServer.StreamRequestHandler):
"Handles one request to mirror some text."

def handle(self):
total_data=[]

line = True
while line:
line = self.rfile.readline().strip()
total_data.append(line)

receivedCommand = '\n'.join(total_data)

newUpdate = cPickle.loads(receivedCommand)
print type(newUpdate)
for item in newUpdate.items:
print str(type(item)) + " with filename: " + item.filename


if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print 'Usage: %s [hostname] [port number]' % sys.argv[0]
sys.exit(1)
hostname = sys.argv[1]
port = int(sys.argv[2])
server = SocketServer.ThreadingTCPServer((hostname, port),
RequestHandler)
server.serve_forever()
[/END CODE]

So I can create the UpdateCommand object on the client, send it across
the wire and I get as far as the line
"newUpdate = cPickle.loads(receivedCommand)",
which when it runs produces the following error:

Traceback (most recent call last):
File "C:\Python25\lib\SocketServer.py", line 464, in
process_request_thread
self.finish_request(request, client_address)
File "C:\Python25\lib\SocketServer.py", line 254, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python25\lib\SocketServer.py", line 522, in __init__
self.handle()
File "C:\Documents and Settings\dwatrous\My Documents\projects\POP
\svn\pop\lib\server.py", line 29, in handle
newUpdate = cPickle.loads(receivedCommand)
ImportError: No module named UpdateCommand

I import the module at the top of the file server.py, but it doesn't
throw the ImportError until it tries to unpickle.

Please help with any ideas that you have.
 
G

Gabriel Genellina

[BEGIN CODE]
#!/usr/bin/python
import SocketServer
import os, sys
newpath = os.path.normpath( os.path.join( __file__, "../../.." ))
sys.path.insert(0, newpath)

from pop.command.UpdateCommand import *
import cPickle


Traceback (most recent call last):
[...]
ImportError: No module named UpdateCommand

I import the module at the top of the file server.py, but it doesn't
throw the ImportError until it tries to unpickle.

Notice that you don't import the UpdateCommand module - you import all
names defined inside it instead. It's not the same thing.
See http://effbot.org/zone/import-confusion.htm
 
D

Daniel

En Tue, 30 Sep 2008 18:38:19 -0300, Daniel <[email protected]>  
escribió:


[BEGIN CODE]
#!/usr/bin/python
import SocketServer
import os, sys
newpath = os.path.normpath( os.path.join( __file__, "../../.." ))
sys.path.insert(0, newpath)
from pop.command.UpdateCommand import *
import cPickle
Traceback (most recent call last):
[...]
ImportError: No module named UpdateCommand
I import the module at the top of the file server.py, but it doesn't
throw the ImportError until it tries to unpickle.

Notice that you don't import the UpdateCommand module - you import all  
names defined inside it instead. It's not the same thing.
Seehttp://effbot.org/zone/import-confusion.htm

Thank you Gabriel,

The class inside that module has the same name, UpdateCommand. Since
this is the object that was pickled, it should be available to the
unpickle command. I already understood the difference between import
methods and I think I'm covered. I did just try "import
pop.command.TesterUpdateCommand" instead and I get the same error.
 
G

Gabriel Genellina

En Tue, 30 Sep 2008 18:38:19 -0300, Daniel <[email protected]>  
escribió:


[BEGIN CODE]
#!/usr/bin/python
import SocketServer
import os, sys
newpath = os.path.normpath( os.path.join( __file__, "../../.." ))
sys.path.insert(0, newpath)
from pop.command.UpdateCommand import *
import cPickle
Traceback (most recent call last):
[...]
ImportError: No module named UpdateCommand
I import the module at the top of the file server.py, but it doesn't
throw the ImportError until it tries to unpickle.

Notice that you don't import the UpdateCommand module - you import all  
names defined inside it instead. It's not the same thing.
Seehttp://effbot.org/zone/import-confusion.htm

Thank you Gabriel,

The class inside that module has the same name, UpdateCommand. Since
this is the object that was pickled, it should be available to the
unpickle command. I already understood the difference between import
methods and I think I'm covered. I did just try "import
pop.command.TesterUpdateCommand" instead and I get the same error.

(TesterUpdateCommand != UpdateCommand...)

In your *pickling* code, just before pickling the object, see what you get
from this:

cls = obj.__class__
print cls.__module__
print cls.__name__

Suppose you get "SomeModuleName" and "SomeClassName". Then, in your
*unpickling* environment, this must succeed:

import SomeModuleName
cls = SomeModuleName.SomeClassName

If not, you should rearrange things (on both sides, probably) to make the
reference work. This is basically what pickle does.

Looks like the module lives in a package - make sure you import the
*package* both when pickling and unpickling. The sys.path manipulation
looks suspicious.
 
D

Daniel

En Tue, 30 Sep 2008 19:44:51 -0300, Daniel <[email protected]>  
escribió:


En Tue, 30 Sep 2008 18:38:19 -0300, Daniel <[email protected]>  
escribió:
[BEGIN CODE]
#!/usr/bin/python
import SocketServer
import os, sys
newpath = os.path.normpath( os.path.join( __file__, "../../.." ))
sys.path.insert(0, newpath)
from pop.command.UpdateCommand import *
import cPickle
Traceback (most recent call last):
[...]
ImportError: No module named UpdateCommand
I import the module at the top of the file server.py, but it doesn't
throw the ImportError until it tries to unpickle.
Notice that you don't import the UpdateCommand module - you import all  
names defined inside it instead. It's not the same thing.
Seehttp://effbot.org/zone/import-confusion.htm
Thank you Gabriel,
The class inside that module has the same name, UpdateCommand.  Since
this is the object that was pickled, it should be available to the
unpickle command.  I already understood the difference between import
methods and I think I'm covered.  I did just try "import
pop.command.TesterUpdateCommand" instead and I get the same error.

(TesterUpdateCommand != UpdateCommand...)

In your *pickling* code, just before pickling the object, see what you get  
 from this:

         cls = obj.__class__
         print cls.__module__
         print cls.__name__

Suppose you get "SomeModuleName" and "SomeClassName". Then, in your  
*unpickling* environment, this must succeed:

         import SomeModuleName
         cls = SomeModuleName.SomeClassName

If not, you should rearrange things (on both sides, probably) to make the  
reference work. This is basically what pickle does.

Looks like the module lives in a package - make sure you import the  
*package* both when pickling and unpickling. The sys.path manipulation  
looks suspicious.

This turned out to be a problem with PyScripter. When I open the same
files in Komodo they work fine.

Sorry for the trouble.
 

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
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top