472,328 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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
4 7363
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: x-herbert | last post by:
Hi, I have a small test to "compile" al litle script as a WMI-Tester. The script include a wmi-wrapper and "insert" the Win32-modeles. here...
2
by: Chris Hodapp | last post by:
I have seen messages posted about this before, and there is a clear reference to it in the manual, but I have been unable to find a solution. ...
2
by: Sebastian Stelzer | last post by:
Hi, I'am a beginner in Python and I have got a problem with the TKInter module. When I want to start e.g. pysol, I get following output: ...
12
by: Georg Brandl | last post by:
Hello, in follow-up to the recent "dictionary accumulator" thread, I wrote a little module with several subclassed dicts. Comments (e.g. makes...
0
by: Bill Davy | last post by:
Hello, I am using SWIG-1.3.24 to make an extension (called SHIP) to Python2.4.1 and then running under IDLE (if that makes any difference) but...
1
by: Spam sucks | last post by:
hello, i create a logging xml file with dom that could have an unknown count of results now it is 0 to 7 but it could be i have 14 or 50 results...
1
by: Laszlo Zsolt Nagy | last post by:
Hi All! I have a running service (a small web server) implemented in python, running as a win32 service. I wrote another program that is very...
6
by: Laszlo Zsolt Nagy | last post by:
Sorry, I realized that the import zlib was not executed from my (working) service. So here is the question: why can't I use zlib from a win32...
1
by: Prof Rodney Coates | last post by:
Although I have been using Macs for a quarter of a century and was doing scientific programming in the '60's I am VERY new to Python on the Mac. I...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.