0

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'

2 Answers 2

1
+100

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

from ..myitems import CarItem
Sign up to request clarification or add additional context in comments.

9 Comments

Hi Fazlul. When I add your answer and then debug it, I get the following error: runspider: error: Unable to load 'c:\\scrapy\\hw_spiders\\spiders\\keizer.py': attempted relative import with no known parent package Do you know how to fix that?
Hi@ Adam, It's hard to guess without full scrapy project. When you will use project then you have to use meaning you have to use project and terminal run command is as follows: scrapy crawl name of your spider, this is my present thinking. Thanks
Hi Fazlull. When I run scrapy crawl <spidername> -o allobjects.json I get error twisted.internet.error.ReactorNotRestartable When I debug and step through I get the mentioned error after all spider code has been executed. My full project is here file.io/MTKJEckHgoal (password Fazlul), if you're willing and have time to take a look :)
Hi@ Adam, scrapy project meaning hw_spiders and BOT_NAME = 'hw' in settings.file aren't the same. They must be the same meaning hw_spiders and BOT_NAME = 'hw'_spiders. And it may remain more errors. After 2-3 days I will free to deeply analize it. Create new project and debug, don't override any scrapy project files
and gain more knowledge from online only for scrapy project portion
|
-1

Scrapy should be installed in your current used python environment.

Open an integrated Terminal and run

pip show scrapy

Check if its location is current environment\lib\site-packages.

If not, please reinstall it by running pip install scrapy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.