cannot import name '_psutil_linux'

ponponon 2021-09-19 14:24:26 阅读数:318

import psutil_linux psutil linux

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

版权声明:本文为[ponponon]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/09/20210919142426315c.html