pythonrelative-import

Module not found error in Visual Studio Code


At design time and using autocomplete, I can add the line from myitems import CarItem.

However, when I run my spider scrapy crawl keizer -o allobjects.json,

I get error:

ModuleNotFoundError: No module named 'myitems'

Output:

  File "C:\scrapy\hw_spiders\spiders\keizer.py", line 11, in <module>
    from myitems import CarItem
ModuleNotFoundError: No module named 'myitems'

My folder structure:

enter image description here

My files:

keizer.py

import json
import re
import os

import scrapy
import time
from scrapy_splash import SplashRequest
from scrapy.selector import Selector
from scrapy.http import HtmlResponse

from myitems import CarItem

Not sure if it's relevant, I also added this to ".vscode\settings.json" file:

{
    "python.analysis.extraPaths": [
        "./hw_spiders"
    ]
}

I already checked here and read about relative imports, but I don't know how to map the proposed solutions to my current project structure as they're quite different. If my project structure is wrong or not recommended, I'd love to hear that too.

Another attempt

I changed from myitems import CarItem to from .myitems import CarItem. I immediately see a design time error:

Import ".myitems" could not be resolved (PylancereportMissingImports)

But I ran the spider anyway.

When I run C:\scrapy\hw_spiders> scrapy crawl keizer -o allobjects.json, I get:

File "C:\scrapy\hw_spiders\spiders\keizer.py", line 11, in from .myitems import CarItem ModuleNotFoundError: No module named 'hw_spiders.spiders.myitems'

When I run C:\scrapy> scrapy crawl keizer -o allobjects.json, I get:

File "C:\scrapy\hw_spiders\spiders\keizer.py", line 11, in from .myitems import CarItem ModuleNotFoundError: No module named 'hw_spiders.spiders.myitems'


Solution

  • If the myitems folder is in the same directory folder, Then try as follows:

    from ..myitems import CarItem