python无法识别导入pyserial/serial,但依赖项已安装[Mac]

python无法识别导入pyserial/serial,但依赖项已安装[Mac],python,pyserial,Python,Pyserial,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 permis

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
Maxs MacBook:~grax$sudo pip install pyserial
当前用户不拥有目录“/Users/grax/Library/Caches/pip/http”或其父目录,缓存已被禁用。请检查该目录的权限和所有者。如果使用sudo执行pip,您可能需要sudo的-H标志。
当前用户不拥有目录“/Users/grax/Library/Caches/pip”或其父目录,并且已禁用缓存控制盘。检查该目录的权限和所有者。如果使用sudo执行pip,您可能需要sudo的-H标志。
已满足要求:pyserial-in/Library/Python/2.7/site-packages
Maxs MacBook:~grax$python
Python 2.7.15(默认值,2018年5月2日,00:53:27)
[GCC 4.2.1达尔文兼容苹果LLVM 9.1.0(clang-902.0.29.1]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入pyserial
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
导入错误:没有名为pyserial的模块

我该怎么办?

python模块被称为
serial
,尽管您称之为pip pyserial。是的,令人困惑

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版本(Python2.7.15)不是MacOSX附带的版本,因此它可能在其他地方寻找python模块

$ python
>>> import sys
>>> print sys.path
pip
的版本很可能不是安装在那里的(pip不是MacOS的原生版本,因此它可能使用
/usr/local/bin/python
/usr/local/lib/python2.7/site包

您可以使用
--target
选项强制pip安装到其他地方:

$ sudo pip install --target /usr/local/lib/python2.7/site-packages pyserial
这将把
serial
模块放在python正在查找的位置