关于python:cannot-import-name-psutillinux

55次阅读

共计 1124 个字符,预计需要花费 3 分钟才能阅读完成。

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 抵触

正文完
 0