导入序列时,我收到以下错误:
Traceback (most recent call last):
File "main.py", line 4, in <module>
import serial # library for communicating through Serial (used to connect to Arduino)
File "/usr/local/lib/python2.7/dist-packages/serial/__init__.py", line 1, in <module>
from serial import properties, meta, model, utilities, errors
File "/usr/local/lib/python2.7/dist-packages/serial/properties.py", line 7, in <module>
from serial import meta, model
File "/usr/local/lib/python2.7/dist-packages/serial/meta.py", line 12, in <module>
from serial import model
File "/usr/local/lib/python2.7/dist-packages/serial/model.py", line 15, in <module>
from serial import properties, meta
ImportError: cannot import name properties
我已经安装了包。我想不明白。请帮忙。
答案 0 :(得分:1)
您希望使用pyserial
,但pyserial没有properties.py
,没有meta.py
而没有model.py
,请参阅:https://github.com/pyserial/pyserial/tree/master/serial所以堆栈跟踪你'显示不是来自pyserial。
所以另一个包和pyserial之间存在冲突。
在Python中隔离工作环境的日常解决方案是使用virtual environment,例如:
$ cd /tmp/
$ mkdir -p ~/.venvs/
$ python3 -m venv ~/.venvs/my_project_using_pyserial/
$ . ~/.venvs/my_project_using_pyserial/bin/activate
(my_project_using_pyserial) $ python3 -m pip install pyserial
Collecting pyserial
Downloading pyserial-3.4-py2.py3-none-any.whl (193kB)
100% |████████████████████████████████| 194kB 2.2MB/s
Installing collected packages: pyserial
Successfully installed pyserial-3.4
(my_project_using_pyserial) $ python3
Python 3.5.4 (default, Aug 12 2017, 14:08:14)
[GCC 7.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> dir(serial)
['CR', 'EIGHTBITS', 'FIVEBITS', 'LF', 'PARITY_EVEN', 'PARITY_MARK', 'PARITY_NAMES', 'PARITY_NONE', 'PARITY_ODD', 'PARITY_SPACE', 'PosixPollSerial', 'SEVENBITS', 'SIXBITS', 'STOPBITS_ONE', 'STOPBITS_ONE_POINT_FIVE', 'STOPBITS_TWO', 'Serial', 'SerialBase', 'SerialException', 'SerialTimeoutException', 'Timeout', 'VERSION', 'VTIMESerial', 'XOFF', 'XON', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'basestring', 'importlib', 'io', 'iterbytes', 'os', 'portNotOpenError', 'protocol_handler_packages', 'serial_for_url', 'serialposix', 'serialutil', 'sys', 'time', 'to_bytes', 'unicode', 'writeTimeoutError']