0

I am 100% new to Python I am bumping into this weird issue regarding compatibility when switching between Windows and Linux. I implemented a small program that implements a TCP/IP socket that communicates with a device that supports server socket over TCP/IP. My program works fine when running on Windows so I decided to test it on Linux because ultimately I want to run it on CRONTAB as a scheduled task grabbing information from a server and export to an XML. This is the weird compilation error I received when trying to compile the same code on Linux (under python 2.6, 2.7 as well as 3.1)

python2.7 weatherScript.py Traceback (most recent call last): File "weatherScript.py", line 1, in <module> import socket File "/media/SWAP/weatherData/socket.py", line 117, in <module> except socket.error, msg: AttributeError: 'module' object has no attribute 'error' 

The corresponding code segments for that would be

import socket import sys import re from time import sleep from xml.dom.minidom import Document 

and

try: comSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, msg: sys.stderr.write("[ERROR] %s\n" % msg[1]) sys.exit(1) #connecting to weather station try: comSocket.connect(()) except socket.error, msg: sys.stderr.write("[ERROR] %s\n" % msg[1]) sys.exit(2) 

Even when I remove the try-catch, the problem still persists. I am not sure whether there is any compilation incompatibility between Windows or Linux or not. Any help?

Other question would be: I want to run that python program under CRONTAB, is there any thing that I should change or include so that I can run it as a "script" or such?

Thank you so much!

1
  • It's weird that this doesn't work on Python 3.1, could you share more about your directory structure and module organization? In which directory is weatherScript.py located, what is your sys.path (or at least the part of it that corresponds to weatherData/socket.py)? Commented Jun 16, 2011 at 20:56

1 Answer 1

2

You have a module named socket, and you're trying to import the socket module from it. The module is obviously shadowing the builtin socket module (i.e. your socket is importing itself). If this module isn't a package, move it to a package. If/when the module is already in a package (I assume weatherData is a package), ensure that you have the following line before any other code:

from __future__ import absolute_import 

This makes sure that all imports done from the module are absolute, i.e. socket is the top-level socket module, while your module is always accessed through the name weatherData.socket because it is a part of that package, and the two modules wouldn't clash.

Also this will only work if weatherData is indeed a package, if socket is a top-level module you need to either rename it, or move it to a package, because otherwise the names would clash regardless of whether relative imports are allowed, and you won't be able to use the two modules together.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't quite get your answer. with import socket, I am actually following the socket documentation on python.org, what it does (I believe) is importing the built-in socket module. The code works perfectly fine on Windows, but I don't know why it's not working on Linux.
weatherData is just a folder where I put the python program.
If you have a socket.py file in the directory where the program is located, you can't import the builtin socket module, because your socket.py is shadowing it. docs.python.org/tutorial/modules.html
I have another question for you sir! So my weatherData.py is supposed to write an xml file that contain the sensor readings from the weather stations. The problem I am bumping into is that: when I don't have that file, my program complied and run flawlessly, but if I have that file there, my program raises weird compilation error. I will put the error by editing my original post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.