0

I have a python file and a helper json file. The json is loaded via

with open("scraper_dbs.json") as f:
    scraper_dbs = json.load(f)

From my testfolder I want to test the file which looks like

from path.to.file import function

On this import statement I get the error FileNotFoundError: [Errno 2] No such file or directory: 'scraper_dbs.json'

When I run the python code itself it finds the json and continues on. What do I miss?

  • Is scraper_dbs.json present along side the test file ? – Naveen Dec 19 '22 at 16:54
  • Where are you running your tests from? From the main directory or the test directory? If you're running the tests from the test directory your relative path should go up till the main directory first. Personally I always prefer to run the tests from the main dir. – Zephyrus Dec 19 '22 at 16:55
  • 1
    try to open `os.path.join(os.path.dirname(__file__), 'scraper_dbs.json')` instead. – Cpt.Hook Dec 19 '22 at 17:09
  • Reply of @Cpt.Hook did the trick. Thanks a ton – FrancoisDuCoq Dec 20 '22 at 09:35

0 Answers0