0

I am trying to import local modules in python but for some reason it cannot find the modules. I know that you can import them by adding them to your path with sys. But I don't want to use sys for this. My file structure looks like this

scraper_backend
    - jobs
        - extract.py
        - load.py
        - models.py
        - transform.py
        - url_builder.py
main.py

my main.py looks like this.

from datetime import datetime, timedelta
from scraper_backend.jobs import extract, load, transform


def main():
    # Extract
    wind = extract.extract_wind(datetime.now())
    solar = extract.extract_solar(datetime.now())
    # Transform
    date = extract.round_dt(datetime.now()) - timedelta(minutes=15)
    df = transform.update_file(date, wind, solar)
    # Load
    load.write_to_path(df, "energy.parquet")

main()

For the moment im using

sys.path.append("scraper_backend//jobs")

But when I remove the sys.path.append it gives me ModuleNotFoundError: No module named 'scraper_backend.jobs. Anyone knows what I'm doing wrong? Thanks for your help.

david backx
  • 163
  • 1
  • 9

1 Answers1

1

You need to add a file called __init__.py in the jobs folder. This tells python that the directory is a package which can be imported.

For example, if you have the following directory structure:

scraper_backend
    - jobs
        - __init__.py
        - extract.py
        - load.py
        - models.py
        - transform.py
        - url_builder.py

Then you can import the jobs package like this:

from scraper_backend.jobs import extract, load, transform

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Branch
  • 88
  • 7