0

I have the following project structure:

root
    /scripts
        script.py
    /scrapers
        scraper.py
    /helpers
        helper.py

How can I import helper.py in script.py or scraper.py?

The only way I got this to work was by keeping all scripts and scrapers inside the same folder and move helpers folder inside that folder with a __init__.py in it, like this:

root
    /lib
        script.py
        scraper.py
        /helpers
            __init__.py
            helper.py

Is there a better way?
I'm on VS Code with Python 3.9.0 and using venv.

Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
iamdlm
  • 1,885
  • 1
  • 11
  • 21

1 Answers1

0

As per this question you can do the following:

If you're in any other file and you want to import helper.py and vice-versa.

import sys
sys.path.append('../')

import helpers.helper as helper
Aasim Sani
  • 339
  • 2
  • 6
  • Tried with and without `__init__.py` inside the helpers folder but still getting the error `no name 'helper1' in module 'helpers'`. – iamdlm Dec 20 '20 at 22:44
  • You'll have to have the ```__init__.py``` and do either ```from helpers.helper import helper1``` or with the import as in the code above you can reference it like this in your code ``` helper.helper1```. – Aasim Sani Dec 21 '20 at 13:33