Python Forum

Full Version: Calling main in project structure fails
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm totally confused about project organization.

I organized my project with a structure like this:


bricks
  --source
    --game_objects
    --types
  + level.py
  + main.py
  --tests
    --game_objects
    --types
Each of these folders and subfolders contains a __init__.py.

Now in the main.py i import like this:


from bricks.source.level import Level
from bricks.source.level import read_level_from_json_file
Then I call a function like this:

def main():
    level = read_level_from_json_file("../level/1.json")
    print(level)


if __name__ == "__main__":
    main()
From the root folder bricks I tried to call the main.py like this but i get import errors:


(virtual) sandro@sandropc:~/Documents/Bricks_py/bricks$ python3 source/main.py
Traceback (most recent call last):
  File "source/main.py", line 1, in <module>
    from bricks.source.level import Level
ModuleNotFoundError: No module named 'bricks'
(virtual) sandro@sandropc:~/Documents/Bricks_py/bricks$ 
What can I do here to fix this?


I already could run unittests with pytest. In the unit tests in the test folder I also import relative like this:


from bricks.source.game_objects.ball import Ball
from bricks.source.types.point import Point
from bricks.source.types.angle import Angle