1

OS: macOS 11.7.1 (Big Sur)

A few months ago I purchased a TCLab kit and at the time did some very rudimentary tests where the device worked as expected.

Recently I decided that I wanted to work on some of the APMonitor lessons and connected the TCLab to my computer expecting that it would work as it had done in the past.

Sadly, that is not the case. I would like help in correcting the issues identified and getting the TCLab to work again.

Originally, I had been using python 3.9. Since then python 3.10 came out and I installed it.

Using the following script from APMonitor as my test,

$ cat show_T1.py
import tclab
with tclab.TCLab() as lab:
    print(lab.T1)

I got the errors documented below:

$ python --version
Python 3.10.8

$ python show_T1.py
Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/show_T1.py", line 1, in <module>
    import tclab
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv3.10/lib/python3.10/site-packages/tclab/__init__.py", line 2, in <module>
    from .historian import Historian, Plotter
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv3.10/lib/python3.10/site-packages/tclab/historian.py", line 6, in <module>
    from collections import Iterable
ImportError: cannot import name 'Iterable' from 'collections' (/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py)

I was able to find the cause of this problem here: Stack Overflow It is an issue where Iterable was moved to collections.abc from collections.

When I change the script to:

$ cat show_T1.py
import collections.abc
collections.Iterable = collections.abc.Iterable
collections.Mapping = collections.abc.Mapping
collections.MutableSet = collections.abc.MutableSet
collections.MutableMapping = collections.abc.MutableMapping
import tclab
with tclab.TCLab() as lab:
    print(lab.T1)

the import error goes away. However, I now get new errors:

$ python show_T1.py
TCLab version 0.4.9
Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv3.10/lib/python3.10/site-packages/tclab/tclab.py", line 64, in __init__
    self.connect(baud=115200)
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv3.10/lib/python3.10/site-packages/tclab/tclab.py", line 114, in connect
    self.sp = serial.Serial(port=self.port, baudrate=baud, timeout=2)
AttributeError: module 'serial' has no attribute 'Serial'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv3.10/lib/python3.10/site-packages/tclab/tclab.py", line 70, in __init__
    self.sp.close()
AttributeError: 'TCLab' object has no attribute 'sp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/show_T1.py", line 7, in <module>
    with tclab.TCLab() as lab:
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv3.10/lib/python3.10/site-packages/tclab/tclab.py", line 77, in __init__
    raise RuntimeError('Failed to Connect.')
RuntimeError: Failed to Connect.

Sadly, I get almost the same error as above if I revert back to python 3.9: (python 3.9 does not have the Iterable problem, so I reverted back to the original script):

$ python --version
Python 3.9.15

$ python show_T1.py
TCLab version 0.4.9
Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv/lib/python3.9/site-packages/tclab/tclab.py", line 64, in __init__
    self.connect(baud=115200)
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv/lib/python3.9/site-packages/tclab/tclab.py", line 114, in connect
    self.sp = serial.Serial(port=self.port, baudrate=baud, timeout=2)
AttributeError: module 'serial' has no attribute 'Serial'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv/lib/python3.9/site-packages/tclab/tclab.py", line 70, in __init__
    self.sp.close()
AttributeError: 'TCLab' object has no attribute 'sp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/show_T1.py", line 2, in <module>
    with tclab.TCLab() as lab:
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv/lib/python3.9/site-packages/tclab/tclab.py", line 77, in __init__
    raise RuntimeError('Failed to Connect.')
RuntimeError: Failed to Connect.

I know that at least I have connectivity to the device, because when I unplug the USB cable I get an error message that says, correctly, that no arduino is connected:

$ python show_T1.py
TCLab version 0.4.9
--- Serial Ports ---
/dev/cu.Bluetooth-Incoming-Port n/a n/a
Traceback (most recent call last):
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/show_T1.py", line 2, in <module>
    with tclab.TCLab() as lab:
  File "/Users/USER/TClab/arduino/0_Test_Device/Python/venv/lib/python3.9/site-packages/tclab/tclab.py", line 61, in __init__
    raise RuntimeError('No Arduino device found.')
RuntimeError: No Arduino device found.

Below are the python modules I have installed under python 3.10 and 3.9:

python 3.10:

$ pip list
Package            Version
------------------ ---------
blessed            1.19.1
bpython            0.23
certifi            2022.9.24
charset-normalizer 2.1.1
contourpy          1.0.6
curtsies           0.4.1
cwcwidth           0.1.8
cycler             0.11.0
fonttools          4.38.0
future             0.18.2
greenlet           2.0.1
idna               3.4
iso8601            1.1.0
kiwisolver         1.4.4
matplotlib         3.6.2
numpy              1.23.5
packaging          21.3
Pillow             9.3.0
pip                22.3.1
Pygments           2.13.0
pyparsing          3.0.9
pyserial           3.5
python-dateutil    2.8.2
pyxdg              0.28
PyYAML             6.0
requests           2.28.1
scipy              1.9.3
serial             0.0.97
setuptools         65.4.1
six                1.16.0
tclab              0.4.9
urllib3            1.26.13
wcwidth            0.2.5

python 3.9:

$ pip list
Package            Version
------------------ ---------
blessed            1.19.1
bpython            0.23
certifi            2022.9.24
charset-normalizer 2.1.1
contourpy          1.0.6
curtsies           0.4.1
cwcwidth           0.1.8
cycler             0.11.0
docopt             0.6.2
fonttools          4.38.0
future             0.18.2
greenlet           2.0.1
idna               3.4
iso8601            1.1.0
kiwisolver         1.4.4
matplotlib         3.6.2
numpy              1.23.5
packaging          21.3
Pillow             9.3.0
pip                22.3.1
pipreqs            0.4.11
Pygments           2.13.0
pyparsing          3.0.9
pyserial           3.5
python-dateutil    2.8.2
pyxdg              0.28
PyYAML             6.0
requests           2.28.1
scipy              1.9.3
serial             0.0.97
setuptools         65.4.1
six                1.16.0
tclab              0.4.9
urllib3            1.26.13
wcwidth            0.2.5
yarg               0.1.9

NOTE: I have send this issue to support@apmonitor.com

MERM
  • 629
  • 7
  • 21
  • The "failed to connect" issue persists after upgrading to tclab 0.4.10.dev0 (from pip install --upgrade https://github.com/jckantor/TCLab/archive/master.zip ) although this resolved the Iterable issue. – MERM Dec 03 '22 at 02:26
  • I also upgraded the Arduino sketch to the latest from https://github.com/jckantor/TCLab-sketch The "LED 100" test works from the Arduino IDE, but I get the same error message from python. – MERM Dec 03 '22 at 02:44

1 Answers1

1

Serial Connection Issue

This error AttributeError: module 'serial' has no attribute 'Serial' suggests that the package serial or a local file name serial.py has a conflict with pyserial. Rename your file to something else besides serial.py and/or uninstall the serial package (not needed for TCLab). Your pyserial package is the latest version.

pip uninstall serial

The error occurs when there is a local file named serial.py and we import from the pyserial module. Additional common TCLab help issues are posted to the TCLab setup and troubleshooting page.

TCLab Help

Serial Port Permission

If the serial uninstall doesn't fix the problem of allowing a serial connection, one other thing to check is the USB port permission. On Linux, discover the USB port name with ls /dev/tty* Set the permission for that USB connection with the correct name.

sudo chmod a+rw /dev/ttyACM0

Python 3.10 Compatibility

You correctly found the issue with installing the latest version of TCLab for Python 3.10 compatibility. The module developer is still working on the next version of the TCLab package. Until that point, you can either edit the historian.py file (path is in the error message) with a text editor and change from collections import Iterable to from collections.abc import Iterable or install the new package from GitHub:

pip install --upgrade https://github.com/jckantor/TCLab/archive/master.zip

This will be resolved with the next release of TCLab on PyPI.org. The current version is 0.4.9 that does not include Python 3.10 compatibility because of the Iterable package change.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • 1
    John, Thank you for your help on this. I removed serial and made sure I had permissions correct on the /dev/ttys, yet the same error was occurring. However, I tried reinstalling pyserial (pip install --force pyserial) and that did the trick. I think uninstalling serial and reinstalling pyserial is what finally helped. It is working now under python 3.9 (pip version) and 3.10 (with the github version) – MERM Dec 03 '22 at 15:30
  • 1
    Also, you might want to create a TCLab tag for other issues that might come up (hopefully, not too many). I didn't have high enough rep to create it when I made my post, but you do. – MERM Dec 03 '22 at 15:35