我正在嘗試運行一個腳本,該腳本呼叫另一個 python 檔案(連同其整個 github 存盤庫一起復制),但是ModuleNotFoundError:
盡管將檔案放入正確的目錄中,但我得到了一個 This is。
我如何運行它
- 激活我的python3.9虛擬環境
(newpy39) aevas@aevas-Precision-7560:~/Desktop/Dashboard_2021_12$ python3 dashboard.py其中 aevas 是我的用戶名,而 ~/Desktop/Dashboard_2021_12 是檔案夾
運行它不需要額外的引數。
儀表板.py 的匯入
import sys
sys.path.insert(1, 'targetTrack')
# Qt imports
from PyQt5.QtCore import QThread, pyqtSignal
import argparse
import configparser
import platform
import shutil
import time
import cv2
import torch
import torch.backends.cudnn as cudnn
from yolov5.utils.downloads import attempt_download
from yolov5.models.experimental import attempt_load
from yolov5.models.common import DetectMultiBackend
from yolov5.utils.datasets import LoadImages, LoadStreams
from yolov5.utils.general import LOGGER, check_img_size, non_max_suppression, scale_coords, check_imshow, xyxy2xywh
from yolov5.utils.torch_utils import select_device, time_sync
from yolov5.utils.plots import Annotator, colors
from deep_sort_pytorch.utils.parser import get_config
from deep_sort_pytorch.deep_sort import DeepSort
第 1 部分:未找到模型,盡管模型是父檔案夾。
python3 dashboard.py
Traceback (most recent call last):
File "/home/aevas/Desktop/Dashboard_2021_12/dashboard.py", line 25, in <module>
from trackerThread import trackerDeepSORT
File "/home/aevas/Desktop/Dashboard_2021_12/trackerThread.py", line 15, in <module>
from yolov5.models.experimental import attempt_load
File "/home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5/models/experimental.py", line 14, in <module>
from models.common import Conv
ModuleNotFoundError: No module named 'models'
第 2 部分:將 import models.common 更改為 import common 后,即使在同一個檔案夾中也找不到 common !
python3 dashboard.py
Traceback (most recent call last):
File "/home/aevas/Desktop/Dashboard_2021_12/dashboard.py", line 25, in <module>
from trackerThread import trackerDeepSORT
File "/home/aevas/Desktop/Dashboard_2021_12/trackerThread.py", line 15, in <module>
from yolov5.models.experimental import attempt_load
File "/home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5/models/experimental.py", line 14, in <module>
from common import Conv
ModuleNotFoundError: No module named 'common'
這就是檔案夾模型中檔案的樣子

這就是experimental.py 的匯入部分的樣子
# YOLOv5 ?? by Ultralytics, GPL-3.0 license
"""
Experimental modules
"""
import math
import numpy as np
import torch
import torch.nn as nn
from common import Conv
from utils.downloads import attempt_download
我已經查閱了以下鏈接,但無濟于事:
- https://towardsdatascience.com/how-to-fix-modulenotfounderror-and-importerror-248ce5b69b1c
- Python can't find module in the same folder
- ModuleNotFoundError: No module named 'models'
- https://github.com/ultralytics/yolov5/issues/353
I understand that I can change it to import .common and then the module can be successfully imported. However, the next line import utils causes a similar error. utils is on the same level as models. Which means there is going to be a cascade of moduleNotFoundError errors at this rate. I also understand using the folder ()method is inadvisable, hence I did not continue with it.
As I had mentioned that I had copied the entire cloned github repo over, when executed standalone, the github repo works perfectly fine. There are no differences between the experimental.py.
What could be wrong?
uj5u.com熱心網友回復:
問題是models模塊位于/home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5檔案夾中,但是當您從 運行 dashboard.py 時/home/aevas/Desktop/Dashboard_2021_12/,Python 解釋器不會在/home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5檔案夾中查找模塊;它只在/home/aevas/Desktop/Dashboard_2021_12/.
解決此問題的常用方法是將 yolov5 安裝為一個包,但似乎 yolov5 不包含以這種方式安裝所需的代碼。要解決這個問題,您需要讓 Python 知道它需要在 yolov5 檔案夾中查找模塊。
快速而簡單的方法是將檔案夾添加到 PYTHONPATH 環境變數中。
在嘗試匯入模塊之前,將 yolov5 檔案夾動態添加到sys.path是一種稍微不那么快速和骯臟但仍然很丑陋的方法。models您可以通過在dashboard.py 的頂部添加如下內容來做到這一點:
from pathlib import Path
import sys
sys.path.append(str(Path(__file__, "..", "targetTrack", "yolov5").resolve()))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427344.html
