473,383 Members | 1,877 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,383 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 7510
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 the code: my "WMI-Tester.py" ----- import wmi
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. I'm on Slackware 9.1, kernel 2.6.0-test11, using...
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: Traceback (most recent call last): File...
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 it sense to use super), corrections, etc.? Is...
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 when I "import SHIP" I get: >>> import SHIP ...
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 how can you read this out with xsl, with php you...
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 similar to the web server but I cannot start the...
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 service? Is there any way to make it working? ...
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 have been writing some very simple applications and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.