pythonscrapy

Helper functions in another file, ModuleNotFoundError when trying to import


I have a simple Python project using scrapy. My file structure looks like this:

top_level_folder
|-scraper
|--spiders
|---help_functions.py
|---<some more files>
|--items.py
|--pipelines.py
|--settings.py
|--<some more files>

help_functions.py has a couple functions defined, like add_to_items_buffer.

In pipelines.py, I'm attempting to do...

from help_functions import add_to_items_buffer

...

class BlahPipeline:
    def process_item(self, item, spider):
       ...
       add_to_items_buffer(item)
       ...

When I try to run this, I get ModuleNotFoundError: No module named 'help_functions'. Doing from spiders.help_functions import add_to_items_buffer throws a similar error.

What's going on here? I imagine I'm misunderstanding something fundamental about how Python imports work.


Solution

  • This post explained it for me! It's a matter of relative vs absolute imports depending on where the file you're calling lives.