By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Sep 30 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
En Tue, 30 Sep 2008 18:38:19 -0300, Daniel <da************@gmail.com>
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.
See http://effbot.org/zone/import-confusion.htm

--
Gabriel Genellina

Sep 30 '08 #2

P: n/a
On Sep 30, 4:17*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 30 Sep 2008 18:38:19 -0300, Daniel <daniel.watr...@gmail.com*
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

--
Gabriel Genellina
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.
Sep 30 '08 #3

P: n/a
En Tue, 30 Sep 2008 19:44:51 -0300, Daniel <da************@gmail.com>
escribió:
On Sep 30, 4:17*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Tue, 30 Sep 2008 18:38:19 -0300, Daniel <daniel.watr...@gmail.com*
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

--
Gabriel Genellina

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.

--
Gabriel Genellina

Sep 30 '08 #4

P: n/a
On Sep 30, 5:49*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 30 Sep 2008 19:44:51 -0300, Daniel <daniel.watr...@gmail.com*
escribió:
On Sep 30, 4:17*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 30 Sep 2008 18:38:19 -0300, Daniel <daniel.watr...@gmail.com*
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
--
Gabriel Genellina
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.

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

Sorry for the trouble.
Oct 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.