Python Forum
Calling main in project structure fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling main in project structure fails
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Project Structure for Modularity and Reusability with Multiple Entry Points b19wh33l5 0 1,630 Apr-24-2024, 12:21 PM
Last Post: b19wh33l5
  Project structure with a virtual environment gdbengo 1 2,252 Jan-26-2022, 03:22 PM
Last Post: snippsat
  python project structure anne 1 2,387 Aug-01-2020, 07:17 PM
Last Post: deanhystad
  Even if my file is "main.py",cannot run my project sylas 13 18,672 Mar-05-2018, 09:15 AM
Last Post: sylas
  Trouble calling functions in main function RedSkeleton007 6 6,364 Nov-11-2017, 01:22 PM
Last Post: sparkz_alot
  Since PyCharm knows which file is "main", how can I tell it "run this project" sylas 7 10,255 Sep-03-2017, 11:48 AM
Last Post: Larz60+
  Where can I find a little project example using main.py and other files sylas 7 6,235 Aug-31-2017, 02:59 PM
Last Post: metulburr
  project/main.py import from project/bin/APP_NAME Myersj281 3 4,796 Jul-19-2017, 04:41 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020