簡體   English   中英

python 3.10(和 python 3.9)的 TCLab 問題

[英]TCLab issues with python 3.10 (and python 3.9)

操作系統: macOS 11.7.1 (Big Sur)

幾個月前,我購買了一個TCLab套件,當時做了一些非常基本的測試,該設備按預期工作。

最近,我決定學習一些APMonitor課程,並將TCLab連接到我的計算機,希望它能像過去一樣工作。

可悲的是,事實並非如此。 我想幫助糾正已識別的問題並讓TCLab再次工作。

最初,我一直在使用python 3.9 從那以后python 3.10出來了,我安裝了它。

使用APMonitor中的以下腳本作為我的測試,

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

我收到以下記錄的錯誤:

$ 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)

我能夠在這里找到這個問題的原因: Stack Overflow這是一個Iterablecollections移動到collections.abc的問題。

當我將腳本更改為:

$ 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)

導入錯誤消失了。 但是,我現在收到新錯誤:

$ 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.

可悲的是,如果我恢復到python 3.9 ,我會得到與上面幾乎相同的錯誤:(python 3.9 沒有Iterable問題,所以我恢復到原始腳本):

$ 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.

我知道至少我可以連接到設備,因為當我拔下 USB 電纜時,我收到一條錯誤消息,正確地說沒有連接 arduino:

$ 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.

下面是我在python 3.10和3.9下安裝的python模塊:

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

注意:我已將此問題發送至 support@apmonitor.com

串行連接問題

此錯誤AttributeError: module 'serial' has no attribute 'Serial'表明 package serial或本地文件名serial.pypyserial有沖突。 將文件重命名為serial.py以外的其他名稱和/或卸載serial package( TCLab 不需要)。 你的pyserial package 是最新版本。

pip uninstall serial

當有一個名為serial.py的本地文件並且我們從pyserial模塊導入時會發生錯誤。 其他常見的 TCLab 幫助問題已發布到TCLab 設置和故障排除頁面

TCLab 幫助

串口權限

如果serial卸載不能解決允許串行連接的問題,另一件要檢查的事情是 USB 端口權限。 在 Linux 上,使用ls /dev/tty*發現 USB 端口名稱 為該 USB 連接設置正確名稱的權限。

sudo chmod a+rw /dev/ttyACM0

Python 3.10 兼容性

您正確地找到了安裝最新版本的 TCLab 以實現 Python 3.10 兼容性的問題。 模塊開發人員仍在開發 TCLab package 的下一個版本。在此之前,您可以使用文本編輯器編輯historian.py文件(路徑在錯誤消息中)並將from collections import Iterable更改為from collections.abc import Iterable或從 GitHub 安裝新的 package:

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

這將在PyPI.org 上的下一版 TCLab 中得到解決。 當前版本為0.4.9 ,由於Iterable package 更改,不包括 Python 3.10 兼容性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM