gpt4 book ai didi

python - import pyserial/serial 无法被 python 识别,但依赖项已安装 [Mac]

转载 作者:行者123 更新时间:2023-12-01 01:45:05 25 4
gpt4 key购买 nike

已安装 pyserial 的依赖项,Python 中无法识别调用 pyserial

Maxs-MacBook:~ grax$ sudo pip install pyserial
The directory '/Users/grax/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/grax/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: pyserial in /Library/Python/2.7/site-packages
Maxs-MacBook:~ grax$ python
Python 2.7.15 (default, May 2 2018, 00:53:27)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.29.1] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyserial
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Import Error: No module named pyserial

我应该做什么?

最佳答案

即使您调用 pip pyserial,Python 模块也称为 serial。是的,令人困惑。

import serial

另一个问题可能是您使用的 python 实例与安装的 pip 实例不同。

查看 pip 安装模块的位置:

$ pip show pyserial
Name: pyserial
Version: 3.4
Summary: Python Serial Port Extension
Home-page: https://github.com/pyserial/pyserial
Author: Chris Liechti
Author-email: cliechti@gmx.net
License: BSD
Location: /Library/Python/2.7/site-packages

因为您的位置是 /Library/Python2.7... pip 似乎正在安装在系统目录中。

但是,您使用的 python 版本 (Python 2.7.15) 不是 MacOSX 附带的版本,因此它可能正在其他地方寻找 python 模块。

$ python
>>> import sys
>>> print sys.path

pip 的版本很可能安装在那里。 (pip 不是 MacOS 原生的,因此它可能使用 /usr/local/bin/python/usr/local/lib/python2.7/site-packages .

您可以使用 --target 选项强制 pip 安装在其他位置:

$ sudo pip install --target /usr/local/lib/python2.7/site-packages pyserial

这会将 serial 模块放在 python 正在查找的位置。

关于python - import pyserial/serial 无法被 python 识别,但依赖项已安装 [Mac],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51461266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com