0

i have a folder format which looks like this:

|-checkers with AI
  |-checkers
    |-__init__.py
    |-board.py
    |-constants.py
|-__init__.py
|-main.py

the code i am using to import this in board.py is: from constants import BLACK, ROWS, RED, SQUARE_SIZE

and for main.py: from checkers.constants import WIDTH, HEIGHT from checkers.board import Board

for whatever reason im getting this error, i have spoken to other people and no one seems to have any idea what im doing wrong. i have tried moving all the code into main and running it without importing, doing it that way works but is very messy.

So my code isn't the problem, i have added __init__.py into each and have added if __name__ == '__main__': main() in main.py now i am getting the error: ModuleNotFoundError: No module named 'constants' anyone know anything?

DrApple
  • 1
  • 3
  • Try importing like from .constants import – Ankzious Jul 13 '22 at 10:09
  • i think its missing a level, move the main.py in first checkers folder – Devyl Jul 13 '22 at 10:16
  • main.py is already in the "checkers with AI" file and i have changed "from constants" to "from .constants" and that has fixed the issue, i tried that before but for some reason it works now. Thanks! – DrApple Jul 13 '22 at 10:27
  • This isn't exactly the same as your question, but the top-voted answer is very informative: https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time – slothrop Jul 13 '22 at 10:28

0 Answers0