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
衝突