欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中ImportError錯誤的詳細解決方法

 更新時間:2022年07月15日 14:56:49   作者:SpikeKing  
最近辛辛苦苦安裝完了python,最后再運行的時候會出現(xiàn)錯誤,所以這篇文章主要給大家介紹了關于Python中ImportError錯誤的詳細解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

ImportError: No module named XXX 錯誤解決

問題

錯誤:ImportError: No module named XXX

在Python的工程中,偶爾會遇到文件無法引用的情況,即PyCharm IDE支持文件跳轉,但是當文件執(zhí)行時,找不到模塊。

例如,錯誤如下:

Traceback (most recent call last):
  File "utils/average_clinic.py", line 17, in <module>
    from utils.file_utils import listdir_files, show_string
ImportError: No module named file_utils

解決

首先,在py文件的頂部,引入根目錄,如

import os
import sys

p = os.path.dirname(os.path.dirname((os.path.abspath(__file__))))
if p not in sys.path:
    sys.path.append(p)

# print p  # 顯示路徑

其次,檢查文件的Working directory,位置:

第一步是導入工程路徑,第二步是檢查文件的工作目錄。優(yōu)先檢查工作目錄,其次檢查工程路徑。同時,要注意PyCharm自動生成文件引用不一定正確,如:

from file_utils import listdir_files, show_string
# 錯誤的引用,文件位于同一目錄下,不使用包名
# from utils.file_utils import listdir_files, show_string

補充:ImportError: libGL.so.1: cannot open shared object file: No such file or directory

報錯信息

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

報錯分析

在使用opencv-python的過程中,只是通過pip安裝了cv相關的python包,但是沒有關注cv對于底層系統(tǒng)中部分共享庫的依賴。這個在docker中是一個常見的錯誤。

解決方案

方案1: 安裝cv2的依賴

這里只是舉例部分環(huán)境,至于具體需要安裝哪些庫,對應的庫名稱,需要參考你本身的系統(tǒng)查詢后,方可執(zhí)行。

apt-get install ffmpeg libsm6 libxext6 -y

方案2: 安裝python3-opencv或者opencv-python-headless

可以通過安裝python3-opencv或opencv-python-headless庫來實現(xiàn)便捷的處理依賴。

apt-get install python3-opencv -y
apt-get install opencv-python-headless -y

總結

到此這篇關于Python中ImportError錯誤的文章就介紹到這了,更多相關Python ImportError錯誤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論