0

I have the following folder-structure

premier_league/
 |_cli_stats/
 |__ __init__.py
 |__cli_stats.py
 |__get_data/
 |__get_stats.py
 |__get_id.py
 |__api_scraper/
 |__api_scraper.py

In cli_stats.py I have the following import:

from get_data.get_stats import SeasonStats

In get_stats.py I have the following import:

from api_scraper.api_scraper import Football.

When running python cli_stats.py from the cli_stats folder the following error occurs.

 File "cli_stats.py", line 36, in <module>
 from get_data.get_stats import SeasonStats
 File "/Users/name/Desktop/Projekt/premier_league_api/cli_stats/get_data/get_stats.py", line 12, in <module>
 from api_scraper.api_scraper import Football
ModuleNotFoundError: No module named 'api_scraper'

But when running python get_stats.py from the get_data folder, the import is successful. Why does the import not work when running cli_stats.py from the cli_stats folder?

asked Jun 9, 2020 at 11:52

1 Answer 1

1

You have to adjust the import to a relativ one. From theget_stats.pyyou have to step into the directory. The error is that from api_scraper.api_scraper import Football is an absolut import.

Try: in get_stats.py

from .api_scraper.api_scraper import Football

(1 dot before the api_scraper)

answered Jun 9, 2020 at 13:21
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for taking your time, that solved the problem. Could you perhaps point me to any resource where where when to use relative/absolut imports are explained? Thank you for the help!
I would recommend the official site: docs.python.org/3/reference/import.html
Otherwise this might be helpfull: realpython.com/absolute-vs-relative-python-imports
Thank you for taking your time to provide the resources, highly appreciated!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.