psutil 是一个第三方包,需要单独安装。

起因,我没有使用 ubuntu20 自带的 python3.8 ,而是再安装了一个 python3.9 。
python3.8 和 3.9 共存就需要我们自己解决冲突问题了。
Python 3.9.5 (default, May 19 2021, 11:32:47) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/psutil/__init__.py", line 95, in <module>
    from . import _pslinux as _psplatform
  File "/usr/lib/python3/dist-packages/psutil/_pslinux.py", line 26, in <module>
    from . import _psutil_linux as cext
ImportError: cannot import name '_psutil_linux' from partially initialized module 'psutil' (most likely due to a circular import) (/usr/lib/python3/dist-packages/psutil/__init__.py)
>>> exit

解决方法:

pip install psutil -U

为什么要用 -U 参数呢

In [1]: import sys

In [2]: sys.path
Out[2]: 
['/home/bot/.local/bin',
 '/usr/lib/python39.zip',
 '/usr/lib/python3.9',
 '/usr/lib/python3.9/lib-dynload',
 '',
 '/home/bot/.local/lib/python3.9/site-packages',
 '/usr/local/lib/python3.9/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/home/bot/.local/lib/python3.9/site-packages/IPython/extensions',
 '/home/bot/.ipython']

In [3]: exit

因为 python 解释器在找包的时候会优先搜索用户目录下的包,我们装在自己的解释器环境下可以避免和系统环境下的 psutil 冲突


universe_king
3.4k 声望680 粉丝