iT邦幫忙

0

ModuleNotFoundError

  • 分享至 

  • xImage

小弟最近在學習 scrapy
在使用 items.py 的時候碰到 ModuleNotFoundError
使用 pycharm 進行 improt 的時候並不會報錯
執行 scrapy crawl 的時候才會產生錯誤訊息
想上來版上詢問

以下是檔案目錄結構、發生錯誤的那行 code 以及錯誤訊息
https://ithelp.ithome.com.tw/upload/images/20211221/20144024wUwbs0KEeE.png

  • spiders 目錄下的 huashan.py
    https://ithelp.ithome.com.tw/upload/images/20211221/20144024yhMf05tw5K.jpg
PS C:\Users\bear\Desktop\traveler\crawler> scrapy crawl huashan
Traceback (most recent call last):
  File "C:\Users\bear\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\bear\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\bear\Desktop\traveler\venv\Scripts\scrapy.exe\__main__.py", line 7, in <module>
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\cmdline.py", line 144, in execute
    cmd.crawler_process = CrawlerProcess(settings)
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\crawler.py", line 280, in __init__
    super().__init__(settings)
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\crawler.py", line 152, in __init__
    self.spider_loader = self._get_spider_loader(settings)
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\crawler.py", line 146, in _get_spider_loader
    return loader_cls.from_settings(settings.frozencopy())
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\spiderloader.py", line 67, in from_settings
    return cls(settings)
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\spiderloader.py", line 24, in __init__
    self._load_all_spiders()
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\spiderloader.py", line 51, in _load_all_spiders
    for module in walk_modules(name):
  File "c:\users\bear\desktop\traveler\venv\lib\site-packages\scrapy\utils\misc.py", line 80, in walk_modules
    mod = import_module(path)
  File "C:\Users\bear\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'crawler'
PS C:\Users\bear\Desktop\traveler\crawler>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
huahualiu
iT邦新手 2 級 ‧ 2021-12-22 08:16:28
最佳解答

因為你起始路徑就已經在 Crawler 裡面了
所以自然找不到 crawler 這個資料夾
應該直接改成:

from traveler.items import ExhibitionItem

即可

感謝您的回覆,我這邊有嘗試過您這樣的寫法,terminal 是可以正常運行的,但是 pycharm 仍然會報錯,想詢問是否是 Pycharm 的問題?
PS: 有重開一個專案,目錄名稱有稍微不同
https://ithelp.ithome.com.tw/upload/images/20211225/20144024gisRKUTKxP.png

huahualiu iT邦新手 2 級 ‧ 2021-12-26 22:42:43 檢舉

那可能是你的 huashan.py 放的位置的問題
這個檔案在最裡面
但你想 import 的檔案在最外面
結構有點奇怪

試試把 huashan.py 放到最外面 crawlers 資料夾下看看吧

我要發表回答

立即登入回答