Question

I'm having trouble with a simple twisted program and when run I get the following error:

Traceback (most recent call last):
  File "serial.py", line 1, in <module>
   from twisted.internet.serialport import SerialPort
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/serialport.py", line 22, in <module>
    import serial
  File "/Users/me/Desktop/serial.py", line 1, in <module>
from twisted.internet.serialport import SerialPort
ImportError: cannot import name SerialPort

I did have a bunch of fancy code, but I determined that the following line is the problem:

from twisted.internet.serialport import SerialPort

I'm running Twisted 13.2.0, Python 2.7 both updated as well as PySerial 2.7 installed. So I believe everything is installed and up to date. This error occurs on two different systems.

The following runs without issue:

python -c "from twisted.internet.serialport import SerialPort"

Thanks for your help. Is it some conflict with PySerial?

Was it helpful?

Solution

Look at these lines of the traceback:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/serialport.py", line 22, in <module>
  import serial
File "/Users/me/Desktop/serial.py", line 1, in <module>
  from twisted.internet.serialport import SerialPort

Here you see that .../twisted/internet/serialport.py is doing this:

import serial

This is a line from Twisted. You can guess based on this fact that the serial module the code wants to import is not something that you have written.

Then look at the next file in the traceback: /Users/me/Desktop/serial.py. This tells you that Python found the serial module in your personal desktop directory. import serial is now running code you wrote. Based on the previous surmise, this is likely a bad thing.

Get rid of serial.py (and serial.pyc and serial.pyo if they exist) and try again. It still may not work but the cause of the problem (and therefore the traceback) should at least be different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top